Webpack+vue+vueRouter modular construction of a complete project instance ultra-detailed steps (with screenshots, code, entry)

>Start (confirm that the node environment and npm package management tool have been installed)

1. Create a new project file named vuedemo

2. npm init -y Initialize the project

clipboard.png

> install project dependencies

3. npm install --save vue The latest version of vue is installed by default

4. npm install --save-dev webpack webpack-dev-server Install webpack, webpack-dev-server (a small Node.js Express server)

*Extension: npm install When installing an npm package, there are two command parameters that can write their information into the package.json file, one is npm install --save and the other is npm install --save-dev. The difference is that --save will add the name of the dependent package to the dependencies key of the package.json file, and --save-dev will be added to the devDependencies key of the package.json file.
--save-dev is what you depend on when developing, - -save is something you still depend on after you publish. *

clipboard.png

5. npm install --save-dev babel-core babel-loader babel-preset-es2015 Install babel. The function of babel is to compile the grammar of es6 into the grammar es5 recognized by the browser

clipboard.png

6. npm install --save-dev vue-loader vue-template-compiler Components used to parse vue, files with .vue suffix

7. npm install --save-dev css-loader style-loader Used to parse css

Extension: css-loader and style-loader, the two handle different tasks, css-loader enables you to use methods like @import and url(...) to implement the function of require(), style-loader will all the calculated Styles are added to the page, and the combination of the two allows you to embed stylesheets in webpack-packaged JS files.

8. npm install --save-dev url-loader file-loader For packing files and pictures

9. npm install --save-dev sass-loader node-sass Used to compile sass

10. npm install --save-dev vue-router Install routing

> Edit the project directory and add code

11. The file directory is as follows;

clipboard.png

//dist file is produced by executing webpack instructions later, don't care;

//webpack.config.js The configuration file itself is also a standard Commonjs specification module;

//routes.js file put routing configuration file;

//index.html home page entry file

//App.vue is the project entry file.

//main.js This is the core file of the project. The global configuration is configured in this file.

//Commponents directory contains public component header files.

//The views file is placed on the details page;

>code
//webpack.config.js
*Comments:
test: a regular expression matching the extension of the files processed by loaders (required) 
loader: the name of the loader (required) 
include/exclude: manually add files that must be processed (required) folder) or block files (folders) that do not need to be processed (optional);*

var path = require('path')
var webpack = require('webpack')

module.exports = {
    entry: './src/main.js',//值可以是字符串、数组或对象
    output: {
        path: path.resolve(__dirname, './dist'),//Webpack结果存储
        publicPath: '/dist/',//懵懂,懵逼,//然而“publicPath”项则被许多Webpack的插件用于在生产模式和开发模式下下更新内嵌到css、html,img文件里的url值
        filename: 'build.js'
    },
    module: {
        rules: [
            {
                test: /\.vue$/,
                loader: 'vue-loader',
                options: {
                    loaders: {
                    }
                    // other vue-loader options go here
                }
            },
            {
                test: /\.js$/,
                loader: 'babel-loader',
                exclude: /node_modules/
            },
            {
                test: /\.(png|jpg|gif|svg)$/,
                loader: 'file-loader',
                options: {
                    name: '[name].[ext]?[hash]'
                }
            }
            //自己加的
            ,
            {
                test: /\.css$/,
                loader: "style-loader!css-loader"
            }
            ,
            {
                test: /\.scss$/,
                loader: "style-loader!css-loader!sass-loader!"
            }
        ]
    },
    resolve: {
        alias: {
            'vue$': 'vue/dist/vue.esm.js'
        }
    },
    devServer: {//webpack-dev-server配置
        historyApiFallback: true,//不跳转
        noInfo: true,
        inline: true//实时刷新
    },
    performance: {
        hints: false
    },
    devtool: '#eval-source-map'
}

if (process.env.NODE_ENV === 'production') {
    module.exports.devtool = '#source-map'
    // http://vue-loader.vuejs.org/en/workflow/production.html
    module.exports.plugins = (module.exports.plugins || []).concat([
        new webpack.DefinePlugin({
            'process.env': {
                NODE_ENV: '"production"'
            }
        }),
        new webpack.optimize.UglifyJsPlugin({
            sourceMap: true,
            compress: {
                warnings: false
            }
        }),
        new webpack.LoaderOptionsPlugin({
            minimize: true
        })
    ])
}

//routes.js

// 引用模板
import Vue from 'vue';
import Router from 'vue-router';
import indexPage from './components/header.vue'
import homePage from './views/home.vue'
import aboutPage from './views/about.vue'

Vue.use(Router)

export default new Router({
    routes:[
        {
            path:'/',
            component:homePage
        },
        {
            path:'/about',
            component:aboutPage
        }
    ]
})

//index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <div id="appIndex">

    </div>
    <script src="./dist/build.js"></script>
</body>
</html>

//App.vue

<!--App.vue是项目入口文件。-->
<template>
    <div id="app">
        <header-tab></header-tab>
        <h2>{{msg}}</h2>
        <div class="nav-box">
            <p class="nav-list">
                <router-link class="nav-item" to="/">首页</router-link>
                <router-link class="nav-item" to="/about">关于</router-link>
            </p>
        </div>
        <div>
            <router-view></router-view>
        </div>
    </div>
</template>

<script>
import HeaderTab from './components/header.vue';
export default {
  name: 'app',
  data () {
    return {
      msg: 'Welcome to Your Vue.js App'
    }
  },
  components:{
    HeaderTab
  }
}
</script>

<style lang="sass">
   /*这里sass编译正常*/
    $redColor:#f00;
    h2{
        color:$redColor;
    }
    #app {
        text-align: center;
        color: #2c3e50;
        margin-top: 60px;
    }
    h1, h2 {
        font-weight: normal;
    }
    ul {
        list-style-type: none;
        padding: 0;
    }
    li {
        text-align: left;
        margin: 0 10px;
    }
    a {
        color: #42b983;
    }
</style>

//main.js

//main.js这是项目的核心文件。全局的配置都在这个文件里面配置
import Vue from 'vue'
import App from './App.vue'
import router from './routes.js'

import './assets/styles/base.css'
//import './assets/sass/reset.sass'//报错暂时不用sass
Vue.config.debug = true;//开启错误提示

new Vue({
        router,
        el: '#appIndex',
        render: h => h(App)
})

//components

<template>
    <div>
        <h1>共同header</h1>
        <img src="../assets/imgs/logo.png">
    </div>
</template>

Note: Don't forget to add a picture

//The views file is placed on the details page;

about.view

//about.vue
<template>
    <div>about</div>
</template>

home.view

//index.vue
<template>
    <div>
        <ol>
            <li v-for="todo in todos">
                {{ todo.text }}
            </li>
        </ol>
        <button @click="eClick()">事件</button>
    </div>
</template>

<script>
export default {
  name: 'indexP',
  data () {
    return {
       todos: [
          { text: 'Learn JavaScript' },
          { text: 'Learn Vue' },
          { text: 'Build something awesome' }
        ]
    }
  },
  methods:{
    eClick(){
        console.log(9999);
    }
  }
}
</script>

//base.css

h1{
    color: #999;
}

//reset.sass (this is shielded first, there is an error,,)

//$redColor:#f00;
//h2{
//  color:$redColor;
//}

> project run up

execute instruction

webpack

clipboard.png

Execute webpack-dev-server:

webpack-dev-server

clipboard.png

The browser opens the generated link: as I am here http://localhost :8083

Home page details page effect:

clipboard.png

About the details page effect:

clipboard.png

ok, I hope you can run through the project process at one time.
Supplement:
Convert webpack and webpack-dev-server commands to npm commands

安装 across-env:npm install cross-env --save-dev

npm install cross-env --save-dev

package.json file added

"dev": "cross-env NODE_ENV=development webpack-dev-server --open --hot",
"build": "webpack"


clipboard.png

ok, execute the npm command npm run dev, the browser opens a new window (equivalent to pointing the npm dev command to the webpack-dev-server command);

npm run dev

Execute npm run build (equivalent to pointing the npm build command to the webpack command)

npm run build




Problems encountered:

1. An error is reported when the .sass file is imported externally, and the sass syntax in <style lang="sass"></style> compiles normally; (I blocked sass above) The
above ends.

===================2018/01 Notes ============================= ===
vue-cli builds vue project process

1. Download and install the node environment and npm package management tool; (node ​​-v; npm -v to check the version);
2. npm install -g vue-cli; use npm to install vue-cli globally; (vue -V) (1. When installing vue-cli, webpack is already installed.)
3. vue init webpack projectname; generate project template (1. webpack is the template name, here we need to use the packaging function of webpack, so use webpack; 2. projectname is the project name)
4. cd projectname
5. npm run dev
6. npm run bulid
Note: The file directory path cannot be in Chinese, an error will be reported

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325112221&siteId=291194637