Vue脚手架
具体安装步骤:
- 第一步(仅第一次执行):全局安装@vue/cli。
npm install -g @vue/cli
- 第二步:切换到要创建项目的目录,然后使用命令创建项目
vue create xxxx
- 第三步:启动项目
npm run ser
脚手架文件结构
├── node_modules
├── public
│ ├── favicon.ico: 页签图标
│ └── index.html: 主页面
├── src
│ ├── assets: 存放静态资源
│ │ └── logo.png
│ │── component: 存放组件
│ │ └── HelloWorld.vue
│ │── App.vue: 汇总所有组件
│ │── main.js: 入口文件
├── .gitignore: git版本管制忽略的配置
├── babel.config.js: babel的配置文件
├── package.json: 应用包配置文件
├── README.md: 应用描述文件
├── package-lock.json:包版本控制文件
关于不同版本的Vue
- vue.js与vue.runtime.xxx.js的区别:
- vue.js 是完整版的Vue,包含:核心功能 + 模板解析器
- vue.runtime.xxx.js 是运行版的Vue,只包含:核心功能;没有模板解析器
- 因为vue.runtime.xxx.js没有模板解析器,所以不能使用template这个配置项,需要使用render函数接收到的createElement函数去指定具体内容。
vue.config.js配置文件
Vue 脚手架隐藏了所有 webpack 相关的配置,若想查看具体的 webpakc 配置,请使用以下方法:
- 使用
vue inspect > output.js
可以查看到Vue脚手架的默认配置 - 使用vue.config.js可以对脚手架进行个性化定制,详情见:https://cli.vuejs.org/zh
Vue脚手架配置代理
server文件代码:
const express = require('express')
const app = express()
app.use((request,response,next)=>{
console.log('有人请求服务器1了');
console.log('请求来自于',request.get('Host')); // 验证changeOrigin属性
console.log('请求的地址',request.url);// 验证pathRewrite属性
next()
})
app.get('/students',(request,response)=>{
const students = [
{
id:'001',name:'tom',age:18},
{
id:'002',name:'jerry',age:19},
{
id:'003',name:'tony',age:120},
]
response.send(students)
})
app.listen(5000,(err)=>{
if(!err) console.log('服务器1启动成功了,请求学生信息地址为:http://localhost:5000/students');
})
App.vue文件代码:
<template>
<div>
<button @click="getStudents">获取学生信息</button>
</div>
</template>
<script>