目录
vue init webpack xxx
npm install -g vue-cli

vue -V
vue init webpack spa1
注1:cmd命令行窗口显示中文乱码,多是因为cmd命令行窗口字符编码不匹配导致, 修改cmd窗口字符编码为UTF-8,命令行中执行:chcp 65001, 切换回中文:chcp 936, 这两条命令只在当前窗口生效,重启后恢复之前的编码。不能管。
1)Project name:项目名,默认是输入时的那个名称spa1,直接回车
2)Project description:项目描述,直接回车
3)Author:作者,随便填或直接回车
4)Vue build:选择题,一般选第一个
- Runtime + Compiler: recommended for most users //运行加编译,官方推荐
- Runtime-only: about 6KB lighter min+gzip, but templates (or any Vue-specific HTML) are ONLY allowed in .vue files - render functions are required elsewhere//仅运行时
5)Install vue-router:是否需要vue-router,Y选择使用,这样生成好的项目就会有相关的路由配置文件
6)Use ESLint to lint your code:是否用ESLint来限制你的代码错误和风格。N
7)Setup e2e tests with Nightwatch?:是否安装e2e测试 N
8)Should we runnpm installfor you after the project has been created? (recommended) (Use arrow keys)
> Yes, use NPM (选择该项即可)
> Yes, use Yarn
> No, I will handle that myself全部选择好回车就进行了生成项目,出现如下内容表示项目创建完成
#Project initialization finished!
npm install
npm run dev

npm install element-ui -S
注1:使用vue+elementUI创建SPA项目,一般情况下其项目结构组成如下:
* Vue + ESLint + webpack + elementUI + ES6
Vue: 主要框架
* ESLint: 帮助我们检查Javascript编程时的语法错误,这样在一个项目中多人开发,能达到一致的语法
* Webpack: 是一个现代 JavaScript 应用程序的静态模块打包器(module bundler)。整个项目中核心配置
* elementUI: 是基于vue的一套样式框架,里面有很多封装好的组件样式
* ES6: 全称ECMAScript6.0,是JavaScript的下一个版本标准,2015.06发版
注1:在git clone项目的时候,项目文件中并没有node_modules文件夹,为什么呢?
我们知道这个文件中(project_home\node_modules)保存的是我们项目开发中所使用的依赖模块。这个文件夹可能有几百兆大小,如果放到github上,其它人clone的时候会非常慢,这个时候就想到用一个package.json依赖配置文件解决这个问题。这样每个人下载这个项目的时候,只需要进入该项目目录直接npm install npm就会到里面去找需要的函数库,也就是依赖。

const config = require('../config')
index: path.resolve(__dirname, '../dist/index.html'),
- <div class="hello">
- <h1>{{ msg }}h1>
- div>
-
- <script>
- //在es6中一个文件可以默认为一个模块,模块通过export向外暴露接口,实现模块间交互等功能
- //一个文件即模块中只能存在一个export default语句,导出一个当前模块的默认对外接口
- export default {
- name: 'Welcome',
-
- //组件的数据对象必须是函数形式定义
- //在定义data时也可以像HelloWorld中那样不带function关键字,效果是一样的
- //HelloWord中为简写形式
- data: function() {
- return {
- msg: 'Welcome to my App'
- }
- }
- }
- script>
-
- <style scoped>
- h1, h2 {
- font-weight: normal;
- }
- style>
- import Welcome from '@/components/Welcome'
-
- export default new Router({
- routes: [
- {
- path: '/HelloWorld',
- name: 'HelloWorld',
- component: HelloWorld
- },
- {
- path: '/',
- name: 'Welcome',
- component: Welcome
- }
- ]
- })
-
- <div>
- <h1>关于我们h1>
- div>
-
- <script>
- //一个文件即模块中只能存在一个export default语句,导出一个当前模块的默认对外接口
- export default {
- name: 'About',
-
- //组件的数据对象必须是函数形式定义
- //暂时不需要定义数据
- data: function() {
- return {
-
- }
- }
- }
- script>
-
- <style>
- style>
-
- <div>
- <h1>用户管理h1>
- div>
-
- <script>
- //一个文件即模块中只能存在一个export default语句,导出一个当前模块的默认对外接口
- export default {
- name: 'UserMsg',
-
- //组件的数据对象必须是函数形式定义
- //暂时不需要定义数据
- data: function() {
- return {
-
- }
- }
- }
- script>
-
- <style>
- style>
3)在router/index.js中定义路由,配置路由时要注意先导入组件
- import About from '@/views/About'
- import UserMsg from '@/views/UserMsg'
- ....
- {
- path: '/About',
- name: 'About',
- component: About
- },
- {
- path: '/UserMsg',
- name: 'UserMsg',
- component: UserMsg
- }
- <div class="hello">
- <h1>{{ msg }}h1>
- <p>
- <router-link to="/UserMsg">用户管理router-link>
- <router-link to="/About">关于我们router-link>
- p>
-
- div>
-
- <script>
- //在es6中一个文件可以默认为一个模块,模块通过export向外暴露接口,实现模块间交互等功能
- //一个文件即模块中只能存在一个export default语句,导出一个当前模块的默认对外接口
- export default {
- name: 'Welcome',
-
- //组件的数据对象必须是函数形式定义
- //在定义data时也可以像HelloWorld中那样不带function关键字,效果是一样的
- //HelloWord中为简写形式
- data: function() {
- return {
- msg: 'Welcome to my App'
- }
- }
- }
- script>
-
- <div>
- <h1>用户注册h1>
- div>
-
- <script>
- //一个文件即模块中只能存在一个export default语句,导出一个当前模块的默认对外接口
- export default {
- name: 'UserRegister',
-
- //组件的数据对象必须是函数形式定义
- //暂时不需要定义数据
- data: function() {
- return {
-
- }
- }
- }
- script>
-
- <style>
- style>
-
- <div>
- <h1>修改密码h1>
- div>
-
- <script>
- //一个文件即模块中只能存在一个export default语句,导出一个当前模块的默认对外接口
- export default {
- name: 'UserPwdUpdate',
-
- //组件的数据对象必须是函数形式定义
- //暂时不需要定义数据
- data: function() {
- return {
-
- }
- }
- }
- script>
-
- <style>
- style>
- import UserRegister from '@/views/UserRegister'
- import UserPwdUpdate from '@/views/UserPwdUpdate'
- //此处只显示了用户管理部分路由配置,为嵌套路由
- {
- path: '/UserMsg',
- name: 'UserMsg',
- component: UserMsg,
- //注意children单词不要写错,如果写错程序不会报错,但效果出不来!!!!
- //以"/" 开头的嵌套路径会被当作根路径,所以子路由的path不需要添加 "/" !!
- children: [
- {
- path: 'UserRegister',
- name: 'UserRegister',
- component: UserRegister
- },
- {
- path: 'UserPwdUpdate',
- name: 'UserPwdUpdate',
- component: UserPwdUpdate
- }
- ]
- }
-
- <div>
- <h1>用户管理h1>
- <p>
- <router-link to="/UserMsg/UserRegister">用户注册router-link>
- <router-link to="/UserMsg/UserPwdUpdate">修改密码router-link>
- p>
- <div>
-
- <router-view/>
- div>
- div>
-
- <script>
- //一个文件即模块中只能存在一个export default语句,导出一个当前模块的默认对外接口
- export default {
- name: 'UserMsg',
-
- //组件的数据对象必须是函数形式定义
- //暂时不需要定义数据
- data: function() {
- return {
-
- }
- }
- }
- script>