關於第一次使用vue-cli

前段時間終於終於能夠用vue-cli,webpack作個企業站,記一下過程。。。javascript

首先node.js,按照vue官網的步驟命令提示符走一波,網速緣由,因此用的是淘寶鏡像 cnpmcss

# 全局安裝 vue-cli
$ npm install --global vue-cli
# 建立一個基於 webpack 模板的新項目
$ vue init webpack my-project
# 安裝依賴
$ cd my-project
$ npm install
$ npm run dev
而後安裝須要用到的其它插件。
安裝axios npm install 要寫入到package.json 裏面 因此後面加上--save,其它安裝同樣,axios不能使用  Vue.use() 而是  Vue.prototype.$ajax = axios,使用this.$ajax.get(url).then(function(){res}) 
jq npm安裝的是最新版本的 想要使用2.1.4版本的,在json文件找到jq 而後改成本身想用的版本,npm install 從新走一遍。
在webpack.base.conf.js裏面配置jq,最上面加上  var webpack = require("webpack") 下面也要加上
  resolve: {
    extensions: ['.js', '.vue', '.json'],
    alias: {
      'vue$': 'vue/dist/vue.esm.js',
      '@': resolve('src'),
      'jquery': 'jquery'
    }
  }, // 增長一個plugins
    plugins: [
          new webpack.ProvidePlugin({
              $: "jquery",
                      jQuery: "jquery"
          })
    ],

自此jq能夠使用了。html

router傳參部分vue

<router-link :to="{path:'/Hello',query: {Id: item.Id,category:3}}">
                                <img v-bind:src="item.Img" />
                                <span v-text="item.Title"></span>
                            </router-link>

接收參數 this.$route.query.Id  this.$route.query.category; java

項目完成以後config-->index.js裏面dev-->assetsPublicPath的'/'換成‘./’改變一下路徑。 npm run build 上傳服務器。node

生成不在根目錄下的項目jquery

在改換路徑裏面配置webpack

module.exports = {
  build: {
    env: require('./prod.env'),
    index: path.resolve(__dirname, '../dist/ceshi/index.html'),
    assetsRoot: path.resolve(__dirname, '../dist'),
    assetsSubDirectory: 'ceshi/static',
    assetsPublicPath: '/',
    productionSourceMap: false,
    // Gzip off by default as many popular static hosts such as
    // Surge or Netlify already gzip all static assets for you.
    // Before setting to `true`, make sure to:
    // npm install --save-dev compression-webpack-plugin
    productionGzip: false,
    productionGzipExtensions: ['js', 'css'],
    // Run the build command with an extra argument to
    // View the bundle analyzer report after build finishes:
    // `npm run build --report`
    // Set to `true` or `false` to always turn it on or off
    bundleAnalyzerReport: process.env.npm_config_report
  },
  dev: {
    env: require('./dev.env'),
    port: process.env.PORT || 8080,
    autoOpenBrowser: true,
    assetsSubDirectory: 'ceshi/static',
    assetsPublicPath: './',
    proxyTable: {},
    // CSS Sourcemaps off by default because relative paths are "buggy"
    // with this option, according to the CSS-Loader README
    // (https://github.com/webpack/css-loader#sourcemaps)
    // In our experience, they generally work as expected,
    // just be aware of this issue when enabling this option.
    cssSourceMap: false
  }
}

 設置路由:ios

{path:'*',redirect:'/index'}設置重定向

封裝組件:git

 

 

相關文章
相關標籤/搜索