使用webpack创建vue项目并整合elementUI

由于vue在导入elementUI时遇到了许多问题,就像配置maven 依赖一样,遇到了很多问题,以此总结。

可以根据需按目录跳转:

1)环境安装与确认
2)使用webpack创建vue项目
3)整合elementUI
4)引入报错问题

环境确认:

在整合elemenUI环境之前,确认一下 Node.js 环境 和 npm 环境

Node.js环境确定:   node -v
npm环境确定:  npm -v

在确定上面环境以及安装以后,确定webpack是否安装

方法1,npm info webpack

方法2,webpack -v

如果没有出现,则 npm install webpack webpack-cli –g (全局安装),因为

注意:webpack 4x以上,webpack将命令相关的内容都放到了webpack-cli,所以还需要安装webpack-cli;

之后再webpack -v

在这里插入图片描述
webpack -v查看提示信息:

webpack本地安装:npm install webpack webpack-cli --save-dev
再查看版本:webpack -v

Vue环境确认:

检查vue-cli环境:vue -V

我的环境信息:
在这里插入图片描述

webpack创建vue项目:

vue init webpack ${项目名}
Vue build ==> 打包方式,回车即可;
Install vue-router ==> 是否要安装 vue-router,项目中肯定要使用到 所以Y 回车;
Use ESLint to lint your code ==> 是否需要 js 语法检测 目前我们不需要 所以 n 回车;    
Set up unit tests ==> 是否安装 单元测试工具 目前我们不需要 所以 n 回车;
Setup e2e tests with Nightwatch ==> 是否需要 端到端测试工具 目前我们不需要 所以 n 回车;

配置信息:
在这里插入图片描述创建完成:
在这里插入图片描述
进入目录文件夹并运行vue项目

cd demo_01
npm run dev

运行成功,可以再浏览器中查看 localhost:8080 :
在这里插入图片描述

导入elementUI:

导入创建的vue项目到编辑器中:我这里使用的是Hbuilder, 使用本地编辑器导入文件夹即可,idea导入需要下载vue.js插件便于编辑。
目录如下:
在这里插入图片描述
在package.json中查看配置信息: “dependencies” 这里没有elementUI信息
在这里插入图片描述
在cmd中通过cd命令进入你创建的项目位置:

cd demo_01 //demo_01是我创建的项目目录

输入命令,导入安装 elementUI

npm i element-ui -S

这时候在 "dependencies"出现elementUI信息
在这里插入图片描述
在src目录下的main.js import elementUI
在这里插入图片描述

import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
Vue.use(ElementUI)

然后我们进行测试,我们在首页加一个switch组件。找到src目录下的App.vue,复制粘贴下面代码:

<template>
  <div id="app">
    <img src="./assets/logo.png">
	<el-switch
	  v-model="value"
	  active-color="#13ce66"
	  inactive-color="#ff4949">
	</el-switch>
    <router-view/>
  </div>
</template>

<script>
export default {
  name: 'App',
  data(){
    return {
      value: true
    }
  }
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

测试结果如图:
在这里插入图片描述
这样就引入配置ok了。更多的组件可以在elementUI官网查询

引入elementUI报错

1.vue 在使用element-ui时import 'element-ui/lib/theme-chalk/index.css‘时一直报错
在这里插入图片描述
首先确保import ‘element-ui/lib/theme-chalk/index.css’ 引入链接不是 import ‘element-ui/lib/theme-default/index.css’
然后在package.json中确认css-loader是否下载,如果下载,是否在build文件夹webpack.base.conf.js中进行配置
在这里插入图片描述
build-webpack.base.conf.js中module: 里面添加

{
        test: /\.css$/,
        include: [
        /src/,//表示在src目录下的css需要编译
        '/node_modules/element-ui/lib/' //增加此项
        ],
        loader: 'style-loader!css-loader'
      }

在这里插入图片描述
如果css-loader和style-loader也安装了还是没有用再尝试 babel-plugin-import 插件 ,我最后是靠这个解决问题
babel-plugin-import 是一款 babel 插件,它会在编译过程中将 import 的写法自动转换为按需引入的方式

npm i babel-plugin-import -D

这里留下package.json 和 build-webpack.base.conf.js 配置 以防以上问题还是没有解决element-ui导入的最后方法.
只需修改 package.json 中的 name 即可
package.json

{
  "name": "login",
  "version": "1.0.0",
  "description": "A Vue.js project",
  "author": "Tianwell",
  "private": true,
  "scripts": {
    "dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js",
    "start": "npm run dev",
    "build": "node build/build.js"
  },
  "dependencies": {
    "element-ui": "^2.13.0",
    "vue": "^2.5.2",
    "vue-router": "^3.0.1"
  },
  "devDependencies": {
    "autoprefixer": "^7.1.2",
    "babel-core": "^6.22.1",
    "babel-helper-vue-jsx-merge-props": "^2.0.3",
    "babel-loader": "^7.1.1",
    "babel-plugin-syntax-jsx": "^6.18.0",
    "babel-plugin-transform-runtime": "^6.22.0",
    "babel-plugin-transform-vue-jsx": "^3.5.0",
    "babel-preset-env": "^1.3.2",
    "babel-preset-stage-2": "^6.22.0",
    "chalk": "^2.0.1",
    "copy-webpack-plugin": "^4.0.1",
    "css-loader": "^0.28.0",
    "extract-text-webpack-plugin": "^3.0.0",
    "file-loader": "^1.1.4",
    "friendly-errors-webpack-plugin": "^1.6.1",
    "html-webpack-plugin": "^2.30.1",
    "node-notifier": "^5.1.2",
    "optimize-css-assets-webpack-plugin": "^3.2.0",
    "ora": "^1.2.0",
    "portfinder": "^1.0.13",
    "postcss-import": "^11.0.0",
    "postcss-loader": "^2.0.8",
    "postcss-url": "^7.2.1",
    "rimraf": "^2.6.0",
    "semver": "^5.3.0",
    "shelljs": "^0.7.6",
    "uglifyjs-webpack-plugin": "^1.1.1",
    "url-loader": "^0.5.8",
    "vue-loader": "^13.3.0",
    "vue-style-loader": "^3.0.1",
    "vue-template-compiler": "^2.5.2",
    "webpack": "^3.6.0",
    "webpack-bundle-analyzer": "^2.9.0",
    "webpack-dev-server": "^2.9.1",
    "webpack-merge": "^4.1.0"
  },
  "engines": {
    "node": ">= 6.0.0",
    "npm": ">= 3.0.0"
  },
  "browserslist": [
    "> 1%",
    "last 2 versions",
    "not ie <= 8"
  ]
}

build-webpack.base.conf.js

'use strict'
const path = require('path')
const utils = require('./utils')
const config = require('../config')
const vueLoaderConfig = require('./vue-loader.conf')

function resolve (dir) {
 return path.join(__dirname, '..', dir)
}



module.exports = {
 context: path.resolve(__dirname, '../'),
 entry: {
   app: './src/main.js'
 },
 output: {
   path: config.build.assetsRoot,
   filename: '[name].js',
   publicPath: process.env.NODE_ENV === 'production'
     ? config.build.assetsPublicPath
     : config.dev.assetsPublicPath
 },
 resolve: {
   extensions: ['.js', '.vue', '.json'],
   alias: {
     'vue$': 'vue/dist/vue.esm.js',
     '@': resolve('src'),
   }
 },
 module: {
   rules: [
     {
       test: /\.vue$/,
       loader: 'vue-loader',
       options: vueLoaderConfig
     },
     {
       test: /\.js$/,
       loader: 'babel-loader',
       include: [resolve('src'), resolve('test'), resolve('node_modules/webpack-dev-server/client')]
     },
     {
       test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
       loader: 'url-loader',
       options: {
         limit: 10000,
         name: utils.assetsPath('img/[name].[hash:7].[ext]')
       }
     },
     {
       test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
       loader: 'url-loader',
       options: {
         limit: 10000,
         name: utils.assetsPath('media/[name].[hash:7].[ext]')
       }
     },
     {
       test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
       loader: 'url-loader',
       options: {
         limit: 10000,
         name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
       }
     }
   ]
 },
 node: {
   // prevent webpack from injecting useless setImmediate polyfill because Vue
   // source contains it (although only uses it if it's native).
   setImmediate: false,
   // prevent webpack from injecting mocks to Node native modules
   // that does not make sense for the client
   dgram: 'empty',
   fs: 'empty',
   net: 'empty',
   tls: 'empty',
   child_process: 'empty'
 }
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值