1. 下载安装node.js
#查看版本
node -v
npm -v
2. 全局安装cnpm
npm install cnpm -g
3. cnpm安装vue-cli
cnpm instal1 vue-cli-g
#测试是否安装成功#查看可以基于哪些模板创建vue应用程序,通常选择webpack
vue list
4. 生成Vue项目
#控制台在任意文件夹下使用如下命令创建一个名为myvue的项目,一路选no
vue init webpack myvue
5. 运行Vue项目
#进入项目文件夹
cd myvue
#项目中安装必要依赖
npm install
#运行命令
npm run dev
#若有报错修复再运行
npm audit fix
6. 安装webpack
cnpm install webpack -g
cnpm install webpack-cli -g
#测试
webpack -v
webpack-cli -v
7. 测试webpack
-
创建一个新文件夹webpack-study作为项目根目录
-
以管理员权限开启idea并打开该项目
-
在根目录创建一个module目录
-
在该目录创建hello.js和main.js
//hello.js exports.sayHi= function(){//暴露sayHi方法 document.write("<h1>调用hello方法</h1>") }
//main.js var hello = require("./hello.js");//导入hello.js hello.sayHi();//自动调用hellO.js中的方法
-
在根目录创建webpack.config.js
module.exports = { entry:'./module/main.js',//打包入口 output:{//打包输出 filename:"./js/bundle.js"//会自动生成disk文件目录 } }
-
在ideal控制台使用webpack命令进行打包
C:\Users\0\Desktop\javacode\vue\webpack-study>webpack
-
创建index.html并引入生成的bundle.js
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> < title>title</title> </head> <body> <script src="dist/js/bundle.js"></script> </body> </html>
-
直接用浏览器运行查看效果
8. 安装Vue路由
cnpm install vue-router --save-dev
9. 测试Vue路由
-
使用idea打开myvue项目
-
生成除原有的组件和内容
-
在components目录下创建两个新组件Main.vue和Content.vue
<template> <h1>首页组件</h1> </template> <script> export default { name: "Main" } </script> <style scoped> </style>
<template> <h1>内容组件</h1> </template> <script> export default { name: "Content" } </script> <style scoped> </style>
-
安装路由,在src目录下,新建一个文件夹router,新建index.js或router.js,如下
import Vue from 'vue' import Router from 'vue-router' import Content from "../components/Content"; import Main from "../components/Main"; Vue.use(Router) export default new Router({ routes: [{ path:'/content',//访问路径 name:'content', component:Content//页面 }, { path:'/main', name:'main', component:Main } ] })
-
在main.js中配置路由
import Vue from 'vue' import App from './App' //导入上面创建的路由配置目录 import router from './router'//自动扫描文件夹下的路由配置js文件 //来关闭生产模式下给出的提示 Vue.config.productionTip = false; new Vue({ el:"#app", //配置路由 router, components:{App}, template:'<App/>' });
-
在
App.vue
中使用路由<template> <div id="app"> <!-- router-link:默认会被渲染成一个<a>标签,to属性为指定链接 router-view:用于渲染路由匹配到的组件 --> <router-link to="/">首页</router-link> <router-link to="/content">内容</router-link> <router-view></router-view> </div> </template> <script> export default{ name:'App' } </script> <style></style>
-
控制台npm run dev 测试