webpack+vue+vueRouter模块化构建完整项目实例详细步骤-入门篇

前端开发 作者: 2024-08-20 20:45:01
新建项目 开始(确认已经安装node环境和npm包管理工具) 1、新建项目文件名为start_vuedemo 2、npm init -y 初始化项目,我的win7系统,工程在d盘的vue_test_p
"dependencies": {
    "vue": "^2.4.2"
  },"devDependencies": {
    "webpack": "^3.4.1","webpack-dev-server": "^2.6.1"
  }
"dependencies": {
    "babel-core": "^6.25.0"
  }

项目结构

webpack.config.js

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

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

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"'
            }
        }), webpack.optimize.UglifyJsPlugin({
            sourceMap:  webpack.LoaderOptionsPlugin({
            minimize: 
        })
    ])
}

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  Router({
    routes:[
        {
            path:'/'

新建一个index.html

<!DOCTYPE html>
<html>
    head>
        meta charset="UTF-8"title></</bodydiv id="appIndex">

        divscript src="./dist/build.js"script>

App.vue

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

>
import HeaderTab from './components/header.vue;
export default {
  name: appreturn {
      msg: Welcome to Your Vue.js App
    }
  },components:{
    HeaderTab
  }
}
style lang="scss"
    $redColor:#f00;
    h2{
        color:$redColor;
    }
    #app 
        text-align center; #2c3e50
        margin-top 60px
    h1,h2 
        font-weight normal
    ul 
        list-style-type none
        padding 0
    li  left
        margin 0 10px
    a  #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;开启错误提示

 Vue({
        router,el: '#appIndex' h(App)
})

commponents下的header.vue

h1>共同headerimg ="../assets/images/logo.png"
    @import '../assets/sass/common.scss'
>

views文件放详情页面

about.vue

//about.vue
>about>

home.vue

olli v-for="todo in todos">
                {{ todo.text }}
            libutton @click="eClick()">事件button
export indexP {
       todos: [
          { text: 首页第一段文本 },{ text: 首页第二段文本首页第三段文本 }
        ]
    }
  },methods:{
    eClick(){
        console.log(9999);
    }
  }
}
scoped
    olwidth200pxmargin20px auto>

css样式添加

base.css

h1{
    color: #999;
}

reset.scss

$redColor:#f00;
h2{$redColor;
}
webpack-dev-server

    @import '../assets/sass/reset.scss'
>
原创声明
本站部分文章基于互联网的整理,我们会把真正“有用/优质”的文章整理提供给各位开发者。本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
本文链接:http://www.jiecseo.com/news/show_65712.html