基于Vue-cli的Vue单页面项目构建详解

在使用vue来构建项目时,通常采用vue脚手架工具vue-cli来构建项目,在项目构建完成之后,需要安装一些常用的插件:jquery、element-ui,以及一些全局配置文件。具体操作如下:

1. 利用脚手架工具vue-cli 下载vue项目模板

// vue init 模板名 项目名
vue init webpack myvuepjt

2. 安装项目依赖

npm install

3. 启动项目,检查项目能否正常运行

在完成上述步骤后,一个vue项目就能正常启用了。接下来安装jquery、element-ui:

1)安装jquery

1. 下载jquery依赖
npm install jquery --save
2. 配置
  webpack.base.conf.js文件中
  ① const webpack = require("webpack")
  ② module.exports 最后中添加
  plugins: [
    new webpack.optimize.CommonsChunkPlugin('common.js'),
    new webpack.ProvidePlugin({
      jQuery: "jquery",
      $: "jquery"
    })
  ]
  ③ 项目入口文件main.js中引入
  import $ from 'jquery'
  Vue.prototype.$ = $
  使用即可

webpack.base.conf.js:

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

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'
  },
  plugins: [
    new webpack.optimize.CommonsChunkPlugin('common.js'),
    new webpack.ProvidePlugin({
      jQuery: "jquery",
      $: "jquery"
    })
  ]
}

2) 安装element-ui

1. npm安装依赖
npm install element-ui --save-dev
2. 引入element-ui
  main.js文件中
  完整引用方式
  import Vue from 'vue'
  import ElementUI from 'element-ui'
  import 'element-ui/lib/theme-chalk/index.css'
  Vue.use(ElementUI)
  按需引用方式参考官网
  http://element.eleme.io/#/zh-CN/component/quickstart

全局文件引入:

1)请求接口文件urlconfig.js

'use strict'
export default {
  requrl: function () {
    var baseurl = 'request_baseurl'
    return {
      url: {
        name: '名称',
        url: baseurl + 'req'
      },
      
    }
  }
}

在main.js中

// 全局引入url配置文件
import RequestApi from '../config/urlconfig.js'
Vue.prototype.reqpath = RequestApi.requrl()

在项目的任何地方可通过:this.reqpath.url.url 引入请求链接

2)全局方法的引入global.js

const getCookie = function (name) {
  var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)")
  if (arr = document.cookie.match(reg)) {
    return unescape(arr[2])
  } else
    return null
}
const nowtoken = getCookie("token")
const rcitlang = getCookie("rcit_environment")
export default {
  nowtoken,
  rcitlang
}

在main.js中

import methods from './assets/js/global.js'
Vue.prototype.methods = methods

在项目的任何地方,可以通过this.methods 调用方法

到此,基于vue-cli构建一个vue项目所需要的一些常用配置都已完成。本文结。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值