简体   繁体   English

如何使用 Vue3 导入 jquery-ui css 文件?

[英]How to import jquery-ui css file with Vue3?

I'm a beginner of typescript and VueJs.我是打字稿和 VueJs 的初学者。 Currently, I tried to use jquery-ui with Vue3+Typescript+Electron.目前,我尝试将 jquery-ui 与 Vue3+Typescript+Electron 一起使用。

I finally created working sample above environment.我终于在环境之上创建了工作示例。 Then I tried to use jquery-ui resizable API.然后我尝试使用 jquery-ui resizable API。 But it requires to use css file.但它需要使用 css 文件。 I googled vue and webpack, how to import css, however there are many ways to import css file.我在 google 上搜索了 vue 和 webpack,如何导入 css,但是导入 css 文件的方法有很多。 So, I can't understand how to import css files in Vue's component file.所以,我无法理解如何在 Vue 的组件文件中导入 css 文件。

I refer following article:我参考以下文章:

My code is like following:我的代码如下:

import $ from "jquery";
import "jquery-ui";

import 'jquery-ui/themes/base/theme.css'  // <== here comes error
import 'node_modules/jquery-ui/xxx-theme.css'  // <== this also don't work
@import "~jquery-ui-dist/jquery-ui.theme.css"; // <== this is working, but no effect 

before that I configured following things according to the site Vue.js x TypeScriptのプロジェクトでjQuery UIを使う:在此之前,我根据站点Vue.js x TypeScriptのプロジェクトでjQuery UIo使う配置了以下内容:

// install jquery, jquery-ui and typescript one
$ npm install jquery jquery-ui --save
$ npm install @types/jquery @types/jqueryui --save-dev

// tsconfig.json
{
  "compilerOptions": {
    ...
    "paths": {
      "@/*": [
        "src/*"
      ],
      "jquery-ui": [
        "node_modules/@types/jqueryui/index"
      ]
    },
    ...
  • install jquery-ui-dist安装 jquery-ui-dist
$ npm install jquery-ui-dist --save

// vue.config.js
const path = require("path");
module.exports = {
  configureWebpack: {
    devtool: "source-map",
    optimization: {
      minimize: false
    },
    resolve: {
      alias: {
        // bind version of jquery-ui
        "jquery-ui": "jquery-ui-dist/jquery-ui.js",
        // bind to modules;
        modules: path.join(__dirname, "node_modules")
      }
    }
  }
};

Finally, I could load css files with following procedure.最后,我可以使用以下过程加载 css 文件。

  • Add "jquery-ui-css" alias in vue.config.js.在 vue.config.js 中添加“jquery-ui-css”别名。 This enable vue to import css with import "jquery-ui-css";这使 vue 可以通过import "jquery-ui-css";
    resolve: {
      alias: {
        // ref: https://github.com/jzaefferer/webpack-jquery-ui#using-resolvealias-to-simplify-imports
        // bind version of jquery-ui
        "jquery-ui": "jquery-ui-dist/jquery-ui.js",
        "jquery-ui-css": "jquery-ui-dist/jquery-ui.css",
        // bind to modules;
        modules: path.join(__dirname, "node_modules")
      }
    },
  • In the vue component, import required modules.在 vue 组件中,导入需要的模块。
<script lang="ts">
import $ from "jquery";
import "jquery-ui";
import "jquery-ui-css";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM