使用vue-cli搭建SPA项目

什么是vue-cli?

vue-cli是vue.js的脚手架,用于自动生成vue.js+webpack的项目模板,创建命令如下:
vue init webpack xxx
a、:xxx 为自己创建项目的名称
b、:必须先安装vue,vue-cli,webpack,node等一些必要的环境

安装vue-cli

npm install -g vue-cli
在这里插入图片描述
一:安装成功后,会出现如下文件
D:\initPath
node-v10.15.3-win-x64
node_global
vue
vue.cmd
vue-init
vue-init.cmd
vue-list
vue-list.cmd
二:安装完成之后打开命令窗口并输入 vue -V(注意这里是大写的“V”),如果出现相应的版本号,则说明安装成功。
在这里插入图片描述

使用脚手架vue-cli(2.X版)来构建项目

在这里插入图片描述
启动并访问项目:启动tomcat,并通过浏览器访问项目
cd wsy_spa
npm run dev
在这里插入图片描述
我们把 http://localhost:8080 在浏览器上运行
在这里插入图片描述
得到这张图说明项目已经建成

在建项目过程中出现“一问一答”模式
 1.Project name:项目名,默认是输入时的那个名称spa1,直接回车
 2.Project description:项目描述,直接回车
 3.Author:作者,随便填或直接回车
 4.Vue build:选择题,一般选第一个
      a、Runtime + Compiler: recommended for most users//运行加编译,官方推荐,就选它了

      b、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.Set up unit tests:是否安装单元测试 N
8.Setup e2e tests with Nightwatch?:是否安装e2e测试  N
9.Should we run `npm install` for you after the project has been created? (recommended) (Use arrow keys)
           > Yes, use NPM                    
             Yes, use Yarn
           No, I will handle that myself     //选择题:选第一项“Yes, use NPM”是否使用npm install安装依赖
停止项目添加element-ui模块
 cd C:\Users\wsy\wsy_spa

npm install element-ui -S #-S就是-save的缩写
在这里插入图片描述
注1:使用vue+elementUI创建SPA项目,一般情况下其项目结构组成如下:
Vue + ESLint + webpack + elementUI + ES6
Vue: 主要框架
ESLint: 帮助我们检查Javascript编程时的语法错误,这样在一个项目中多人开发,能达到一致的语法
Webpack: 设置代理、插件和loader处理各种文件和相关功能、打包等功能。整个项目中核心配置
elementUI: 是基于vue的一套样式框架,里面有很多封装好的组件样式
ES6: 全称ECMAScript6.0,是JavaScript的下一个版本标准,2015.06发版

npm install/npm install xxx -S/npm install xxx -D/npm install xxx -g的区别
一、 npm install
     下载“package.json”中dependencies和devdependencies中配置的所有依赖模块,并保存到项目的node_modules目录
     注1:在git clone项目的时候,项目文件中并没有node_modules文件夹,为什么呢?
 二、npm install xxx -g
      全局安装,下载依赖模块,并保存到%node_home%\node_global\node_modules目录下
三、 npm install xxx -S
     写入到package.json的dependencies对象,并保存到项目的node_modules目录
四、 npm install xxx -D
     写入到package.json的devDependencies对象,并保存到项目的node_modules目录
  注1:在git clone项目的时候,项目文件中并没有node_modules文件夹,为什么呢?
       我们知道这个文件中(project_home\node_modules)保存的是我们项目开发中所使用的依赖模块。这个文件夹可能有几百兆大小,
       如果放到github上,其它人clone的时候会非常慢,这个时候就想到用一个package.json依赖配置文件解决这个问题。
       这样每个人下载这个项目的时候,只需要进入该项目目录直接npm install npm就会到里面去找需要的函数库,也就是依赖。
  注2:缩写命令的全称,注意大小写、-S,-D都是大写
       i/install
       -S/--save
       -D/--save-dev
       -g/--global
  注3:package.json文件里面的devDependencies和dependencies对象有什么区别呢?
       devDependencies里面的插件只用于开发环境,不用于生产环境,而dependencies是需要发布到生产环境的。
       例如:gulp ,babel,webpack一般都是辅助工具,应该使用--save-dev安装到开发环境
       例如:vue ,react,应该使用--save 安装到生产环境     
vue项目结构说明
   build文件夹                       这个文件夹主要是进行webpack的一些配置
     webpack.base.conf.js            webpack基础配置,开发环境,生产环境都依赖
     webpack.dev.conf.js             webpack开发环境配置
     webpack.prod.conf.js            webpack生产环境配置
     build.js                        生产环境构建脚本      
     vue-loader.conf.js              此文件是处理.vue文件的配置文件
   config文件夹
     dev.env.js                      配置开发环境
     prod.env.js                     配置生产环境
     index.js                        这个文件进行配置代理服务器,例如:端口号的修改
   node_modules文件夹                存放npm install时根据package.json配置生成的npm安装包的文件夹
   src文件夹                         源码目录(开发中用得最多的文件夹)
     assets                          共用的样式、图片
     components                      业务代码存放的地方,里面分成一个个组件存放,一个页面是一个组件,一个页面里面还会包着很多组件
     router                          设置路由   
     App.vue                         vue文件入口界面
     main.js                         对应App.vue创建vue实例,也是入口文件,对应webpack.base.config.js里的入口配置  
   static文件夹                      存放的文件不会经过webpack处理,可以直接引用,例如swf文件如果要引用可以在webpack配置
     对swf后缀名的文件处理的loader,也可以直接将swf文件放在这个文件夹引用

   package.json                      这个文件有两部分是有用的:scripts 里面设置命令以及在dependencies和devDependencies中,
   分别对应全局下载和局部下载的依赖包
接下来我们就写一个简单的案例
index.js
import Vue from 'vue'
import Router from 'vue-router'
// import HelloWorld from '@/components/HelloWorld'
//这里导入我自己定义的组件 @ 代表src这个目录 写上路径就 objk 了
import About from '@/views/About'
import UserInfo from '@/views/UserInfo'
import UserPwd from '@/views/UserPwd'
import UserDetal from '@/views/UserDetal'


Vue.use(Router)

export default new Router({
  routes: [
    {
      path: '/',
      name: 'About',
      component: About
    },{
      path: '/About',
      name: 'About',
      component: About
    }, {
      path: '/UserInfo',
      name: 'UserInfo',
      component: UserInfo,
      children:[
        {
          path: '/UserPwd',
          name: 'UserPwd',
          component: UserPwd
        },
        {
          path: '/UserDetal',
          name: 'UserDetal',
          component: UserDetal
        }
      ]
    }
  ]
})
About.vue
<template>
  <div class="hello">
    博主个人信息
  </div>
</template>

<script>
  export default {
    name:'About',
    data:function() {
      return {

      };
    }
  }
</script>

<style>
    .hello{
      color: red;
    }
</style>
UserDetal.vue
<template>
  <div class="hello">
    修改密码
  </div>
</template>

<script>
  export default {
    name: 'UserDetal',
    data: function() {
      return {

      };
    }
  }
</script>

<style>
  .hello {
    color: blue;
  }
</style>
UserInfo.vue
<template>
  <div>
    <router-link to="/UserPwd">用户密码</router-link>
    <router-link to="/UserDetal">修改密码</router-link>
    <!-- 这里直接展示效果就objk了 -->
    <router-view />
  </div>
</template>

<script>
  export default {
    name: 'UserInfo',
    data: function() {
      return {

      };
    }
  }
</script>

<style>
  .hello {
    color: blue;
  }
</style>

UserPwd.vue
<template>
  <div class="hello">
    用户密码
  </div>
</template>

<script>
  export default {
    name: 'UserPwd',
    data: function() {
      return {

      };
    }
  }
</script>

<style>
  .hello {
    color: blue;
  }
</style>

App.vue
<template>
  <div id="app">
    <router-link to="/About">关于我们</router-link>
    <router-link to="/UserInfo">用户信息</router-link>
    <router-view/>
  </div>
</template>

<script>
export default {
  name: 'App'
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>
main.js
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'

Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app',
  //这里已经帮我们挂载好了路由器
  router,
  components: { App },
  template: '<App/>'
})

运行可得到
在这里插入图片描述
点击用户信息就可以得到在这里插入图片描述
点击用户密码
在这里插入图片描述
点击修改密码得到
在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

听晚风续过晚

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值