使用Vue-cli搭建SPA项目

什么是vue-cli?

vue-cli是vue.js的脚手架,用于自动生成vue.js+webpack的项目模板
注1:xxx 为自己创建项目的名称
注2:必须先安装vue,vue-cli,webpack,node等一些必要的环境

安装vue-cli

打开cmd依次执行以下命令
npm install -g vue-cli
npm install -g webpack
注1:安装成功后,会出现如下文件
D:\initPath
node-v12.18.3-win-x64
node_global
vue
vue.cmd
vue-init
vue-init.cmd
vue-list
vue-list.cmd

在这里插入图片描述
== 注2:安装完成之后打开命令窗口并输入 vue -V(注意这里是大写的“V”),如果出现相应的版本号,则说明安装成功。==
在这里插入图片描述

构建SPA项目

任意创建一个文件夹,Shift+右键‘’在此处打开命令窗口‘’
注:文件夹不要有中文
在这里插入图片描述

输入命令:vue init webpack spa1
注:spa1:代表创建的项目名
创建项目类似于问答题
“一问一答”模式
1.Project name:项目名,默认是输入时的那个名称spa1,直接回车
2.Project description:项目描述,直接回车
3.Author:作者,随便填或直接回车
4.Vue build:选择题,一般选第一个
4.1Runtime + Compiler: recommended for most users//运行加编译,官方推荐,就选它了
4.2Runtime-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安装依赖
全部选择好回车就进行了生成项目,出现如下内容表示项目创建完成
Project initialization finished!
实在不会选,就回车选择“默认”或是选择“N”不安装

在这里插入图片描述

引入创建的项目HBuilder到里

在这里插入图片描述
在这里插入图片描述
注:vue-cli构建的项目,在控制台npm run dev启动后,默认的调试地址是8080端口为了避免与tomcat起冲突,在项目里的config——index.js更改

'use strict'
// Template version: 1.3.1
// see http://vuejs-templates.github.io/webpack for documentation.
const path = require('path')
module.exports = {
 dev: {

   // Paths
   assetsSubDirectory: 'static',
   assetsPublicPath: '/',
   proxyTable: {},

   // Various Dev Server settings
   host: 'localhost', // can be overwritten by process.env.HOST
   port: 8088, //  这里修改端口号
   autoOpenBrowser: false,
   errorOverlay: true,
   notifyOnErrors: true,
   poll: false, // https://webpack.js.org/configuration/dev-server/#devserver-watchoptions- 
   /**
    * Source Maps
    */
   // https://webpack.js.org/configuration/devtool/#development
   devtool: 'cheap-module-eval-source-map',
   // If you have problems debugging vue-files in devtools,
   // set this to false - it *may* help
   // https://vue-loader.vuejs.org/en/options.html#cachebusting
   cacheBusting: true,
   cssSourceMap: true
 },
 build: {
   // Template for index.html
   index: path.resolve(__dirname, '../dist/index.html'),
   // Paths
   assetsRoot: path.resolve(__dirname, '../dist'),
   assetsSubDirectory: 'static',
   assetsPublicPath: '/',
   /**
    * Source Maps
    */
   productionSourceMap: true,
   // https://webpack.js.org/configuration/devtool/#production
   devtool: '#source-map',
   // Gzip off by default as many popular static hosts such as
   // Surge or Netlify already gzip all static assets for you.
   // Before setting to `true`, make sure to:
   // npm install --save-dev compression-webpack-plugin
   productionGzip: false,
   productionGzipExtensions: ['js', 'css'],
   // Run the build command with an extra argument to
   // View the bundle analyzer report after build finishes:
   // `npm run build --report`
   // Set to `true` or `false` to always turn it on or off
   bundleAnalyzerReport: process.env.npm_config_report
 }
}

package.json详解

每个项目的根目录下面,一般都有一个package.json文件,定义了这个项目所需要的各种模块,
以及项目的配置信息(比如名称、版本、许可证等元数据)。npm install命令根据这个配置文件,
自动下载所需的模块,也就是配置项目所需的运行和开发环境
详情见资料“package-详解.json”中的相关注释

{
  "name": "t243_spa",
  "version": "1.0.0",
  "description": "A Vue.js project",
  "author": "",
  "private": true,
  "scripts": {
    "dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js",
    "start": "npm run dev",
    "build{
	"name": "spa1", //项目名称:1.name中不能包含大写字母name;2.不能以"."(点)或者"_"(下划线)开头
	"version": "1.0.0", //项目版本
	"description": "A Vue.js project", //项目描述
	"author": "", //作者
	"private": true, //如果你不希望授权别人以任何形式使用私有包或未发布的包,设为true这个包将不会发布到NPM平台下
	"scripts": { //指定了运行脚本命令的npm命令行缩写,比如push指定了运行npm run dev时,所要执行的命令
		"dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js",
		"start": "npm run dev",
		"build": "node build/build.js"
	},
	"dependencies": { //指定依赖的其它包,这些依赖是指包发布后正常执行时所需要的,也就是线上需要的包。它们将会被安装在node_module目录下
		"element-ui": "^2.9.1",
		"vue": "^2.5.2",
		"vue-router": "^3.0.1"
	},
	"devDependencies": { //开发环境依赖包列表。这些依赖只有在开发时候才需要,它们将会被安装在node_module目录下
		//NPM使用语义版本号来管理代码,语义版本号分为X.Y.Z三位,分别代表主版本号、次版本号和补丁版本号
		//当代码变更时,版本号按以下原则更新。
		// 1.如果只是修复bug,需要更新Z位。
		// 2.如果是新增了功能,但是向下兼容,需要更新Y位。
		// 3.如果有大变动,向下不兼容,需要更新X位
		//version,必须匹配某个版本,如:1.1.2,表示必须依赖1.1.2版
		//>version,必须大于某个版本,如:>1.1.2,表示必须大于1.1.2版
		//~version,大概匹配某个版本,~1.1.2,表示>=1.1.2 <1.2.0
		//^version,兼容某个版本,如:^1.1.2 ,表示>=1.1.2 <2.0.0
		"autoprefixer": "^7.1.2",
		"babel-core": "^6.22.1",
		"babel-helper-vue-jsx-merge-props": "^2.0.3",
		"babel-loader": "^7.1.1",
		"babel-plugin-syntax-jsx": "^6.18.0",
		"babel-plugin-transform-runtime": "^6.22.0",
		"babel-plugin-transform-vue-jsx": "^3.5.0",
		"babel-preset-env": "^1.3.2",
		"babel-preset-stage-2": "^6.22.0",
		"chalk": "^2.0.1",
		"copy-webpack-plugin": "^4.0.1",
		"css-loader": "^0.28.0",
		"extract-text-webpack-plugin": "^3.0.0",
		"file-loader": "^1.1.4",
		"friendly-errors-webpack-plugin": "^1.6.1",
		"html-webpack-plugin": "^2.30.1",
		"node-notifier": "^5.1.2",
		"optimize-css-assets-webpack-plugin": "^3.2.0",
		"ora": "^1.2.0",
		"portfinder": "^1.0.13",
		"postcss-import": "^11.0.0",
		"postcss-loader": "^2.0.8",
		"postcss-url": "^7.2.1",
		"rimraf": "^2.6.0",
		"semver": "^5.3.0",
		"shelljs": "^0.7.6",
		"uglifyjs-webpack-plugin": "^1.1.1",
		"url-loader": "^0.5.8",
		"vue-loader": "^13.3.0",
		"vue-style-loader": "^3.0.1",
		"vue-template-compiler": "^2.5.2",
		"webpack": "^3.6.0",
		"webpack-bundle-analyzer": "^2.9.0",
		"webpack-dev-server": "^2.9.1",
		"webpack-merge": "^4.1.0"
	},
	"engines": { //指定包运行的环境
		"node": ">= 6.0.0",
		"npm": ">= 3.0.0"
	},
	//根据提供的目标浏览器的环境来,智能添加css前缀,js的polyfill垫片,来兼容旧版本浏览器
	//last 2 versions:CanIUse.com追踪的IE最新版本为11,向后兼容两个版本即为10、11
	"browserslist": [
		"> 1%",
		"last 2 versions",
		"not ie <= 8"
	]
}

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
==注1:写入到package.json的devDependencies对象,并保存到项目的node_modules目录
在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 ==

vue项目结构说明

build文件夹这个文件夹主要是进行webpack的一些配置
webpack.base.conf.jswebpack基础配置,开发环境,生产环境都依赖
webpack.dev.conf.jswebpack开发环境配置
webpack.prod.conf.jswebpack生产环境配置
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.vuevue文件入口界面
main.js对应App.vue创建vue实例,也是入口文件,对应webpack.base.config.js里的入口配置
static文件夹-存放的文件不会经过webpack处理,可以直接引用,例如swf文件如果要引用可以在webpack配置对swf后缀名的文件处理的loader,也可以直接将swf文件放在这个文件夹引用
package.json这个文件有两部分是有用的:scripts 里面设置命令以及在dependencies和devDependencies中,分别对应全局下载和局部下载的依赖包

案例

案例思路图
在这里插入图片描述

App.Vue

<template>
  <div id="app">
    <!-- <img src="./assets/logo.png"> -->
    <router-link to="/Home">首页</router-link>
    <router-link to="/About">关于</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>

定义组件

注:组件定义在src目录下的components里
Home

<template>
  <div>
    显示博客内容
  </div>
</template>
<script>
</script>
<style>
</style>

About.vue

<template>
  <div>
    <router-link to="/AboutMe">关于博主</router-link>
    <router-link to="/AboutWebSite">关于网站</router-link>
    <router-view/>
  </div>
</template>
<script>
</script>
<style>
</style>

AboutMe.vue

<template>
  <div>
    显示博主相关事迹
  </div>
</template>
<script>
</script>
<style>
</style>

AboutSite.vue

<template>
  <div>
    显示网站来历
  </div>
</template>
<script>
</script>
<style>
</style>

定义完组件后,在src——》router——》index.js建立组件关系

import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import Home from '@/components/Home'
import About from '@/components/About'
import AboutMe from '@/components/AboutMe'
import AboutWebSite from '@/components/AboutWebSite'
Vue.use(Router)

export default new Router({
  routes: [
    {
      path: '/',
      name: 'Home',
      component: Home
    },
    {
      path: '/Home',
      name: 'Home',
      component: Home
    },
    {
      path: '/About',
      name: 'About',
      component: About,
      children:[
        {
          path: '/AboutMe',
          name: 'AboutMe',
          component: AboutMe
        },
        {
          path: '/AboutWebSite',
          name: 'AboutWebSite',
          component: AboutWebSite
        },
      ]
    }
  ]
})

注:例如:import model from “@/common/HelloWorld”,这里路径前面的“@”符号表示什么意思?
答案:@等价于/src这个目录,避免写麻烦又易错的相对路径

效果:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值