vue-cli 项目中引入jquery、sass、md5、ElementUI、vue-cookie

本文介绍了如何在前端项目中配置Sass、jQuery、md5密码加密、ElementUI、vue-cookie及axios等常用模块和框架,并详细说明了安装方式与使用方法。

1,新增 sass 模块

  • 安装:npm install sass-loader node-sass --save-dev
  • .vue文件的style标签改写为:
<style scoped lang="scss"></style>

2,新增 jQuery 模块

  • 安装:cnpm install jquery --save
  • webpack有providePlugin插件,可以自动引入模块。所以需要在项目工程的build文件夹下webpack.base.config.js中添加以下配置
    (1)开头加入 const webpack = require('webpack')
    (2)在最后一个大括号内 加入代码
plugins: [
  new webpack.ProvidePlugin({
    $: "jquery",
    jQuery: "jquery",
    'window.jQuery': 'jquery'
  })
]
  • eslint认为没有声明,需要在eslintrc.js中加入globals配置
globals: {
  '$': false,
  'jquery': false
}

3,新增 md5 密码加密模块

  • 安装:npm install --save js-md5
  • 在 main.js 输入以下代码:
import md5 from 'js-md5'
Vue.prototype.$md5 = md5
  • 在其他 .vue 文件中使用md5:
let password = this.$md5('111')

4,新增 ElementUI 框架

import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
// 上部分为引入ElementUI的css样式
// 若出现问题,可以在index.html文件中引入静态element-ui的静态css文件
Vue.use(ElementUI)

// 注:修改ElementUI主题色网站:https://elementui.github.io/theme-preview/#/zh-CN
  • 在外部 .js 文件中使用 ElementUI 的弹窗功能
import ElementUI from 'element-ui'
let $msgbox = ElementUI.MessageBox
let $alert = ElementUI.MessageBox.alert
let $confirm = ElementUI.MessageBox.confirm
let $prompt = ElementUI.MessageBox.prompt
let $message = ElementUI.Message
// 调用,例:$message.error('1111')

5,新增 vue-cookie 框架

  • 安装:npm install vue-cookie --save
  • main.js文件配置如下:
import VueCookie from 'vue-cookie'
Vue.use(VueCookie)
  • 使用vue-cookie:
this.$cookie.set('userName', '用户名') // 设置 cookie
this.$cookie.get('userName') // 读取 cookie
  • 外部 .js 文件 使用 cookie:
import $cookie from 'vue-cookie'
$cookie.set('userName', 'aaa111') // 设置 cookie
console.log($cookie.get('userName')) // 读取 cookie

6,axios
中文文档:https://www.kancloud.cn/yunye/axios/234845

6.1 安装:npm install axios --save
6.2 新建 fetch.js文件,代码如下

import axios from 'axios'
import router from '@/router'
// elementui
import ElementUI from 'element-ui'
let $msgbox = ElementUI.MessageBox
let $alert = ElementUI.MessageBox.alert
let $confirm = ElementUI.MessageBox.confirm
let $prompt = ElementUI.MessageBox.prompt
let $message = ElementUI.Message

// 创建axios实例
const service = axios.create({
  baseURL: '/api', // 请求地址
  timeout: 5000
})

// request 拦截器
service.interceptors.request.use(config => {
  // Do something before request is sent
  return config
}, error => {
  console.log(error)
  Promise.reject(error)
})

// response 拦截器
service.interceptors.response.use(
  // response => response
  response => {
    const code = response.data.code
    console.log('返回数据' + JSON.stringify(response.data))

    // 验证错误码
    if (code === 0) {
      return response.data
    }
  },
  error => {
    console.log('err' + error) // for debug
    $message({
      message: '错误:' + error.message,
      type: 'error',
      duration: 5 * 1000
    })
    return Promise.reject(error)
  }
)

export default service

6.3 编写接口

import fetch from '@/util/fetch'
export function getData() {
	let data = {
		no: '18600000000',
		value: '222'
	}
	return fetch({
		method: 'post',
		url: '/login',
		data
	})
}

6.4 在组件中调用接口

import { getData } from '接口地址'
getData().then(res => {
	console.log(res)
})

6,npm install --save 与 npm install --save-dev区别
以npm安装msbuild为例
1,npm install msbuild:
(1)会把msbuild安装到node_modules目录中
(2)不会修改 pack.json
(3)之后运行 npm install命令时,不会自动安装msbuild

2,npm install msbuild --save:
(1)会把msbuild包安装到node_modules目录中
(2)会在package.json的dependencies属性下添加msbuild
(3)之后运行npm install 命令时,会自动安装msbuild到node_modules目录中
(4)之后运行npm install --production 或者注明NODE_ENV变量值为production时,会自动安装msbuild到node_modules目录中

3,npm install msbuild --save-dev:
(1)会把msbuild包安装到node_modules目录中
(2)会在package.json的devDependencies属性下添加msbuild
(3)之后运行npm install命令时,会自动安装msbuild到node_modules目录中
(4)之后运行npm install --production或者注明NODE_ENV变量值为production时,不会自动安装msbuild到node_modules目录中

4,使用原则:
运行时需要用到的包使用–save,否则使用–save-dev。

{ "name": "vue-antd-jeecg", "version": "2.4.3", "private": true, "scripts": { "pre": "cnpm install || yarn --registry https://registry.npmmirror.com || npm install --registry https://registry.npmmirror.com ", "serve": "vue-cli-service serve", "build:test": "vue-cli-service build --mode test", "build": "vue-cli-service build", "lint": "vue-cli-service lint", "analyzer": "set analyzer=true && vue-cli-service build " }, "dependencies": { "@antv/data-set": "^0.11.8", "@jeecg/antd-onine": "^1.0.1", "@jeecg/antd-online-mini": "2.4.3-RC", "@tinymce/tinymce-vue": "^2.1.0", "@toast-ui/editor": "^2.5.4", "ant-design-vue": "^1.7.8", "area-data": "^5.0.6", "axios": "^0.28.0", "clipboard": "^2.0.11", "codemirror": "^5.65.5", "cssnano-preset-default": "^7.0.2", "dayjs": "^1.11.3", "dom-align": "1.12.0", "echarts": "^5.5.0", "enquire.js": "^2.1.6", "html2canvas": "^1.4.1", "jquery": "^3.5.0", "js-cookie": "^2.2.0", "jsonp": "^0.2.1", "jspdf": "^2.5.1", "lodash.get": "^4.4.2", "lodash.pick": "^4.4.0", "md5": "^2.3.0", "nprogress": "^0.2.0", "tinymce": "^7.0.0", "viser-vue": "^2.4.8", "vue": "^2.6.14", "vue-area-linkage": "^5.1.0", "vue-cookies": "^1.8.1", "vue-cropper": "^0.5.8", "vue-easy-printer": "^1.0.6", "vue-i18n": "^8.27.1", "vue-loader": "^16.8.3", "vue-ls": "^3.2.2", "vue-photo-preview": "^1.1.3", "vue-print-nb-jeecg": "^1.0.11", "vue-router": "^3.5.4", "vue-splitpane": "^1.0.4", "vue-to-pdf": "^1.0.0", "vuedraggable": "^2.24.3", "vuex": "^3.6.2", "vxe-table": "3.7.10", "vxe-table-plugin-antd": "1.8.10", "xe-utils": "2.4.8", "yarn": "^1.22.19" }, "devDependencies": { "@babel/polyfill": "^7.12.1", "@vue/cli-plugin-babel": "^3.3.0", "@vue/cli-plugin-eslint": "^3.3.0", "@vue/cli-service": "^3.3.0", "@vue/eslint-config-standard": "^4.0.0", "babel-eslint": "7.2.3", "compression-webpack-plugin": "^3.1.0", "eslint": "^5.16.0", "eslint-plugin-vue": "^5.1.0", "html-webpack-plugin": "^4.5.2", "less": "^3.13.1", "less-loader": "^4.1.0", "node-sass": "^4.11.0", "sass-loader": "^7.0.1", "vue-template-compiler": "^2.6.14", "webpack": "^4.46.0", "webpack-bundle-analyzer": "^4.5.0" }, "eslintConfig": { "root": true, "env": { "node": true }, "extends": [ "plugin:vue/strongly-recommended", "@vue/standard" ], "parserOptions": { "parser": "babel-eslint" }, "rules": { "generator-star-spacing": "off", "no-mixed-operators": 0, "vue/max-attributes-per-line": [ 2, { "singleline": 5, "multiline": { "max": 1, "allowFirstLine": false } } ], "vue/attribute-hyphenation": 0, "vue/html-self-closing": 0, "vue/component-name-in-template-casing": 0, "vue/html-closing-bracket-spacing": 0, "vue/singleline-html-element-content-newline": 0, "vue/no-unused-components": 0, "vue/multiline-html-element-content-newline": 0, "vue/no-use-v-if-with-v-for": 0, "vue/html-closing-bracket-newline": 0, "vue/no-parsing-error": 0, "no-tabs": 0, "indent": [ "off", 2 ], "no-console": 0, "space-before-function-paren": 0 } }, "postcss": { "plugins": { "autoprefixer": {} } }, "browserslist": [ "> 1%", "last 2 versions", "not ie <= 10" ] } 帮我设置版本合理,使依赖之间不会冲突,使用的使vue2
09-09
{ "name": "ims-web-view", "version": "0.1.0", "private": true, "scripts": { "serve": "vue-cli-service serve", "build": "vue-cli-service build", "test:unit": "vue-cli-service test:unit", "analyze": "cross-env ANALYZE=true vue-cli-service build", "build:dameng": "vue-cli-service build --mode dameng", "build:mofan": "vue-cli-service build --mode mofan", "build:prod": "vue-cli-service build", "build:stage": "vue-cli-service build --mode staging", "build:sxf": "vue-cli-service build --mode sxf", "build:uat": "vue-cli-service build --mode uat" }, "dependencies": { "@babel/runtime": "^7.25.7", "@fullcalendar/core": "^6.1.15", "@fullcalendar/daygrid": "^6.1.15", "@fullcalendar/interaction": "^6.1.15", "@fullcalendar/resource-timeline": "^6.1.15", "@fullcalendar/timegrid": "^6.1.15", "@fullcalendar/timeline": "^6.1.15", "@fullcalendar/vue": "^6.1.15", "@jiaminghi/data-view": "^2.10.0", "@riophae/vue-treeselect": "^0.4.0", "@sentry/tracing": "^7.118.0", "@sentry/vue": "^7.118.0", "@stomp/stompjs": "^7.0.0", "@tencentcloud/call-uikit-vue": "^4.0.7", "@tencentcloud/call-uikit-vue2": "^4.0.7", "@tencentcloud/chat-uikit-vue": "^2.5.1", "@vant/area-data": "^1.5.1", "alloyfinger": "^0.1.16", "awe-dnd": "^0.3.4", "axios": "^1.7.7", "core-js": "^3.38.1", "dayjs": "^1.11.13", "dhtmlx-gantt": "^8.0.10", "dom-to-image-hm": "^2.6.1", "dompurify": "^3.2.5", "echarts-wordcloud": "^2.1.0", "el-table-infinite-scroll": "^2.0.2", "element-resize-detector": "^1.2.4", "element-ui": "^2.15.14", "file-saver": "^2.0.5", "html2canvas": "^1.4.1", "jquery": "^3.7.1", "js-cookie": "^3.0.5", "js-md5": "^0.8.3", "jsencrypt": "^3.3.2", "jsonwebtoken": "^9.0.2", "jspdf": "^1.5.3", "jszip": "^3.10.1", "lodash": "^4.17.21", "moment": "^2.30.1", "node-forge": "^1.3.1", "normalize.css": "^8.0.1", "nprogress": "^0.2.0", "path-to-regexp": "^6.3.0", "posthog-js": "^1.230.4", "qrcodejs2": "0.0.2", "qs": "^6.13.0", "quill-image-extend-module": "^1.1.2", "recorder-core": "^1.2.23020100", "recordrtc": "^5.6.2", "save": "^2.4.0", "screenfull": "^6.0.2", "sm-crypto": "^0.3.13", "sortablejs": "^1.15.3", "svg-sprite-loader": "^6.0.11", "svgo": "^1.3.2", "swiper": "^5.4.5", "tim-upload-plugin": "^1.4.2", "trtc-sdk-v5": "^5.1.3", "turndown": "^7.2.0", "v-viewer": "^1.6.4", "vant": "^2.12.54", "vue": "^2.7.16", "vue-amap": "^0.5.10", "vue-awesome-swiper": "^4.1.1", "vue-bus": "^1.2.1", "vue-carousel": "^0.18.0", "vue-class-component": "^7.2.3", "vue-clipboard2": "^0.3.3", "vue-clock2": "^1.1.5", "vue-count-to": "^1.0.13", "vue-gallery": "^2.0.1", "vue-i18n": "^8.28.1", "vue-js-toggle-button": "^1.3.3", "vue-markdown": "^2.2.4", "vue-orgchart": "^1.1.7", "vue-property-decorator": "^9.1.2", "vue-qr": "^2.3.0", "vue-quill-editor": "^3.0.6", "vue-router": "^3.6.5", "vue-splitpane": "^1.0.6", "vue-wordcloud": "^1.1.1", "vue2-org-tree": "^1.3.6", "vuex": "^3.6.2", "vxe-table": "^3.11.14", "vxe-table-plugin-export-xlsx": "^2.2.1", "vxe-utils": "^1.9.3", "wangeditor": "^4.7.15", "xe-ajax": "^4.0.3", "xe-utils": "^3.4.0", "xlsx": "^0.18.5" }, "devDependencies": { "@babel/plugin-transform-runtime": "^7.25.7", "@riophae/vue-treeselect": "^0.4.0", "@types/chai": "^4.2.15", "@types/mocha": "^8.2.1", "@types/node": "^16.18.108", "@vue/cli-plugin-babel": "^5.0.8", "@vue/cli-plugin-router": "^5.0.8", "@vue/cli-plugin-typescript": "~5.0.0", "@vue/cli-plugin-unit-mocha": "^5.0.8", "@vue/cli-plugin-vuex": "^5.0.8", "@vue/cli-service": "^5.0.8", "@vue/preload-webpack-plugin": "^2.0.0", "@vue/test-utils": "^1.3.6", "awe-dnd": "^0.3.4", "babel-plugin-component": "^1.1.1", "babel-plugin-import": "^1.13.8", "buffer": "^6.0.3", "chai": "^4.5.0", "compression-webpack-plugin": "^11.1.0", "cross-env": "^7.0.3", "crypto-browserify": "^3.12.1", "css-loader": "^7.1.2", "cssnano": "^7.0.6", "dom-to-image-hm": "^2.6.1", "echarts": "^5.5.1", "file-loader": "^6.2.0", "https-browserify": "^1.0.0", "less": "^4.2.0", "less-loader": "^12.2.0", "lodash": "^4.17.21", "mini-css-extract-plugin": "^2.9.1", "os-browserify": "^0.3.0", "path-browserify": "^1.0.1", "process": "^0.11.10", "sass": "^1.79.4", "sass-loader": "^16.0.2", "sockjs-client": "^1.6.1", "stream-browserify": "^3.0.0", "stream-http": "^3.2.0", "string-replace-loader": "^3.2.0", "terser-webpack-plugin": "^5.3.10", "timers-browserify": "^2.0.12", "typescript": "~4.5.5", "url": "^0.11.4", "url-loader": "^4.1.1", "util": "^0.12.5", "vm-browserify": "^1.1.2", "vue-amap": "^0.5.10", "vue-style-loader": "^4.1.3", "vue-template-compiler": "^2.7.16", "webpack-bundle-analyzer": "^4.10.2" }, "browserslist": [ "> 1%", "last 2 versions", "not dead" ] } 那个是我的node版本这是package.json 文件
07-14
我需要查询的远程的数据库,以下是我目前的依赖,有没有合理的方案,并且告诉我还需要下载什么依赖{ "name": "yueji", "version": "3.0.0", "private": true, "scripts": { "dev": "vue-cli-service serve", "start": "vue-cli-service serve", "serve": "vue-cli-service serve", "build": "vue-cli-service build", "lint": "vue-cli-service lint", "postinstall": "patch-package" }, "dependencies": { "@element-plus/icons-vue": "^2.3.1", "@inotom/vue-curve-text": "^3.0.0", "@planetscale/database": "^1.19.0", "animate.css": "^3.5.1", "axios": "^0.24.0", "buffer": "^6.0.3", "core-js": "^3.6.5", "crypto-browserify": "^3.12.1", "crypto-js": "^4.1.1", "css-color-function": "^1.3.3", "dayjs": "^1.10.6", "echarts": "^5.6.0", "element-plus": "^2.7.2", "file-saver": "^2.0.5", "highlight.js": "^11.6.0", "ip-regex": "^5.0.0", "jquery": "^3.7.1", "js-cookie": "^3.0.5", "markdown-it-abbr": "^2.0.0", "markdown-it-anchor": "^9.2.0", "markdown-it-footnote": "^4.0.0", "markdown-it-highlightjs": "^4.2.0", "markdown-it-sub": "^2.0.0", "markdown-it-sup": "^2.0.0", "markdown-it-task-lists": "^2.1.1", "markdown-it-toc-done-right": "^4.2.0", "marked": "^4.0.13", "md-editor-v3": "^5.5.0", "md5": "^2.3.0", "mysql": "^2.18.1", "mysql2": "^3.14.1", "nprogress": "^0.2.0", "pinyin-pro": "^3.26.0", "qs": "^6.11.2", "recorder-core": "^1.3.25011100", "rgb-hex": "^4.0.0", "screenfull": "^5.1.0", "stream-browserify": "^3.0.0", "url-regex": "^5.0.0", "use-element-plus-theme": "^0.0.5", "v-viewer": "^3.0.13", "vis-network": "^7.5.0", "vue": "^3.4.27", "vue-clipboard3": "^2.0.0", "vue-router": "4.3.2", "vue3-menus": "^1.1.2", "vue3-seamless-scroll": "^2.0.1", "vuex": "^4.1.0" }, "devDependencies": { "@babel/core": "^7.21.8", "@babel/plugin-proposal-nullish-coalescing-operator": "^7.18.6", "@babel/plugin-proposal-optional-chaining": "^7.21.0", "@babel/preset-env": "^7.21.5", "@vue/cli-plugin-babel": "~4.5.0", "@vue/cli-plugin-eslint": "~4.5.0", "@vue/cli-plugin-router": "~4.5.0", "@vue/cli-plugin-vuex": "~4.5.0", "@vue/cli-service": "~4.5.0", "@vue/compiler-sfc": "^3.0.0", "@vue/eslint-config-standard": "^5.1.2", "babel-eslint": "^10.1.0", "babel-loader": "^8.2.5", "compression-webpack-plugin": "^6.1.2", "eslint": "^6.7.2", "eslint-plugin-import": "^2.20.2", "eslint-plugin-node": "^11.1.0", "eslint-plugin-promise": "^4.2.1", "eslint-plugin-standard": "^4.0.0", "eslint-plugin-vue": "^7.0.0", "hard-source-webpack-plugin": "^0.13.1", "linkify-it": "^4.0.1", "markdown-it": "^14.1.0", "patch-package": "^6.4.7", "sass": "^1.87.0", "sass-loader": "^8.0.2", "svg-sprite-loader": "^6.0.11" } }
05-30
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值