目录

VUE

一 脚手架文件结构

├── 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

  1. vue.js与vue.runtime.xxx.js的区别:

    1. vue.js是完整版的Vue,包含:核心功能 + 模板解析器。
    2. vue.runtime.xxx.js是运行版的Vue,只包含:核心功能;没有模板解析器。
  2. 因为vue.runtime.xxx.js没有模板解析器,所以不能使用template这个配置项,需要使用render函数接收到的createElement函数去指定具体内容。

三 vue.config.js配置文件

  1. 使用vue inspect > output.js可以查看到Vue脚手架的默认配置。
  2. 使用vue.config.js可以对脚手架进行个性化定制,详情见:https://cli.vuejs.org/zh

vue文件的基本结构

components

例如Student.vue

<template>
<!-- 组件的交互-->
<div class="demo1">
<h2>学生姓名:{{ name }}</h2>
<h2>学生年龄:{{ age }}</h2>
<button @click="showName">点我提示学生姓名</button>
</div>
<template> <style>
<!-- css样式-->
.demo1{
background: yellow;
}
</style> <script>
//组件交互的代码
//export default school分别暴露
export default {
name: 'Student', //开发者工具最终呈现的名字为School
data(){ // 必须是函数, 返回一个对象
return {
name:'panyue',
age:21
}
},
methods:{ // 该组件中的方法
showName(){
alert(this.name);
}
}
}; //统一暴露
// export { school };
// export default school //默认暴露
</script>

School.vue

<template>
<!-- 组件的交互-->
<div class="demo">
<h2>学校名称:{{ name }}</h2>
<h2>学校地址:{{ address }}</h2>
<button @click="showName">点我提示学校名</button>
</div>
</template> <style>
/*css样式*/
.demo{
background: skyblue;
}
</style> <script>
//组件交互的代码
//export default school分别暴露
export default {
name: 'School', //开发者工具最终呈现的名字为School
data(){
return {
name:'武汉科技大学',
address: '武汉'
}
},
methods:{
showName(){
alert(this.name);
}
}
}; //统一暴露
// export { school };
// export default school //默认暴露
</script>

App.vue

<template>
<!--总体布局 一张图片 School组件和Student组件-->
<div>
<img src="./assets/logo.png" alt="logo">
<School></School>
<Student></Student>
</div>
</template> <script>
// 引入School组件和Student组件, 使用组件必须要引入
import School from './components/School';
import Student from "./components/Student"; export default {
name: "App",
//汇总所有的组件
components:{
Student,
School
}
}
</script>

main.js

import Vue from 'vue'
import App from './App.vue' Vue.config.productionTip = false; new Vue({
render: h => h(App),
}).$mount('#app')

四 ref属性

  1. 被用来给元素或子组件注册引用信息(id的替代者)
  2. 应用在html标签上获取的是真实DOM元素,应用在组件标签上是组件实例对象(vc)
  3. 使用方式:
    1. 打标识(标签\ 组件都可以):<h1 ref="xxx">.....</h1><School ref="xxx"></School>
    2. 获取:this.$refs.xxx

TIPS

  • v-text 更新元素的textContent. 如果要更新部分的textContent, 需要使用 {{ Musthache }}插值.

    <span v-text="msg"></span>
    <!-- 和下面的一样 -->
    <span>{{msg}}</span>
  • methods的方法可以接受一个event参数, 可以自己指定名字(如e), 而event.target代表发生此事件的dom元素.

五 props配置项

  1. 功能:让组件接收外部传过来的数据

  2. 传递数据:<Demo name="xxx"/>

  3. 接收数据:

    1. 第一种方式(只接收):props:['name']

    2. 第二种方式(限制类型):props:{name:String}

    3. 第三种方式(限制类型、限制必要性、指定默认值):

      props:{
      name:{
      type:String, //类型
      required:true, //必要性
      default:'老王' //默认值
      }
      }

    备注:props是只读的,Vue底层会监测你对props的修改,如果进行了修改,就会发出警告(在控制台中),若业务需求确实需要修改,那么请复制props的内容到data中一份,然后去修改data中的数据。

Tips

  • 可以用v-bind ( 来绑定某个要传递的prop, 它可以获取后面表达式的值动态的赋给子组件的props

  • 如果props和data存在同名的属性,会报错,但以props传递的属性值为主

  • 注意props属性名不能是vue底层已征用的属性名(比如key, ref等等)

  • 可以把props传递过来的值当成vc的状态并重命名, 这样该传递来的属性是不会出问题的, 因为没有直接去修改props

    // 父组件
    <Student name="panyue" sex="男" :age="age" /> // 子组件中data
    data() {
    console.log(this)
    return {
    msg:"我是一名普通的大学生",
    myName: this.name
    }
    }

六 mixin(混入)

  1. 功能:可以把多个组件共用的配置提取成一个混入对象

  2. 使用方式:

    第一步定义混合:

    {
    data(){....},
    methods:{....}
    ....
    }

    第二步使用混入:

    ​ 导入混入: import { mixin, shareData } from "@/mixin";

    ​ 全局混入:Vue.mixin(xxx)

    ​ 局部混入:mixins:['xxx']

Example

mixin.js

export const mixin = {
// 方法
methods:{
showName(){
alert(this.name)
}
},
//挂载完毕就执行
mounted() {
console.log('你好啊')
}
} export const shareData = {
data(){
return {
x:100,
y:200
}
}
}

main.js

//引入Vue
import Vue from "vue";
//引入App
import App from './App'; // import { mixin, shareData } from "@/mixin"; //关闭Vue的生产提示
Vue.config.productionTip = false;
//全局混合
// Vue.mixin(mixin);
// Vue.mixin(shareData); new Vue({
el:'#app',
render: h => h(App)
});

App.vue

<template>
<div>
<Student />
<hr />
<School />
</div>
</template> <script>
import Student from "@/components/Student"; // @ 相当于相对路径src
import School from "@/components/School";
export default {
name: "App",
components: {
School,
Student,
},
};
</script>

Student.vue

<template>
<div >
<h2 @click="showName">姓名:{{ name }}</h2>
<h2>性别: {{ sex }}</h2>
</div>
</template> <script>
import { mixin, shareData } from "@/mixin";
export default {
name: "Student",
data(){
console.log(this);
return {
name: '张三',
sex: '男',
x:666 //如果混合中配置了与data(或者配置了相同的methods)相同的属性值,则以你的配置的属性为主(而不以mixin为主)
}
},
mounted() {
console.log('你好啊啊!!!!') //但对于生命周期钩子是都会保存的(混合的钩子比你配置的钩子先跑)
},
mixins:[ mixin, shareData ]
}
</script>

School.vue

<template>
<div >
<h2 @click="showName">学校名称:{{ name }}</h2>
<h2>学校地址: {{ address }}</h2>
</div>
</template> <script>
//引入混合
import { mixin, shareData } from "@/mixin";
export default {
name: "School",
data(){
console.log(this);
return {
name: 'wust',
address: '武汉科技大学'
}
},
mixins:[ mixin, shareData ]
}
</script>

Tips

  • 如果混合中配置了与data(或者配置了相同的methods)相同的属性值,则以你的配置的属性为主(而不以mixin为主)
  • 对于生命周期钩子是都会保存的(混合的钩子比你配置的钩子先跑)

七 插件

  1. 功能:用于增强Vue

  2. 本质:包含install方法的一个对象,install的第一个参数是Vue,第二个以后的参数是插件使用者传递的数据。

  3. 定义插件:

    对象.install = function (Vue, options) {
    // 1. 添加全局过滤器
    Vue.filter(....) // 2. 添加全局指令
    Vue.directive(....) // 3. 配置全局混入(合)
    Vue.mixin(....) // 4. 添加实例方法\属性
    Vue.prototype.$myMethod = function () {...}
    Vue.prototype.$myProperty = xxxx
    }
  4. 使用插件:Vue.use()

Tips:

plugins.js

//vm和vc都可以用
export default {
install(Vue){
//vue帮你调用install方法
// console.log('install');
// console.log(Vue); //vm的构造函数Vue //全局过滤器
Vue.filter('mySlice', function (val){
return val.slice(0,4);
}); //全局指令
Vue.directive('fbind', {
bind(el, binding){
// console.log('bind')
el.value = binding.value;
},
//指令被插入页面时
inserted(el, _){
// console.log('inserted')
el.focus();
},
//指令所在模版被重新解析时
update(el, binding){
// console.log('update');
el.value = binding.value;
}
}); //全局混入
Vue.mixin({
data(){
return {
x:1,
y:2
}
}
}); //给vue原型上添加一个方法 vc/vm都可以使用
Vue.prototype.hello = function (){
alert('hello')
}
}
}

main.js

import Vue from 'vue'
import App from './App.vue'
// 引入插件
import plugins from "./plugins" Vue.config.productionTip = false Vue.use(plugins) // vue使用插件 new Vue({
render: h => h(App),
}).$mount('#app')

School.vue

<template>
<div>
<!-- 南京师范-->
<h2>学校名称: {{ name | mySlice}}</h2>
<h2>学校地址: {{ address}}</h2>
<!-- 会弹出来hello-->
<button @click="test">点我测试一下hello</button>
</div>
</template> <script>
export default {
name:"School",
data() {
return {
name: "南京师范大学",
address: "南京市栖霞区文苑路"
}
},
methods: {
test() {
this.hello();
}
}
}
</script>

Student.vue

<template>
<div>
<h2>姓名: {{ name }}</h2>
<h2>性别: {{ sex }}</h2>
<!-- 相当于v-model 双向绑定-->
<input v-fbind:value="name"/>
</div>
</template> <script>
export default {
name: "Student",
data() {
console.log(this)
return {
name: "Bleak",
sex: "man"
}
}
}
</script>

八 scoped样式

  1. 作用:让样式在局部生效,防止冲突。
  2. 写法:<style scoped>

九 总结TodoList案例

  1. 组件化编码流程:

    ​ (1).拆分静态组件:组件要按照功能点拆分,命名不要与html元素冲突。

    ​ (2).实现动态组件:考虑好数据的存放位置,数据是一个组件在用,还是一些组件在用:

    ​ 1).一个组件在用:放在组件自身即可。

    ​ 2). 一些组件在用:放在他们共同的父组件上(状态提升)。

    ​ (3).实现交互:从绑定事件开始。

  2. props适用于:

    ​ (1).父组件 ==> 子组件 通信

    ​ (2).子组件 ==> 父组件 通信(要求父先给子一个函数)

  3. 使用v-model时要切记:v-model绑定的值不能是props传过来的值,因为props是不可以修改的!

  4. props传过来的若是对象类型的值,修改对象中的属性时Vue不会报错,但不推荐这样做.

Code:

App.vue
<template>
<div id="root">
<div class="todo-container">
<div class="todo-wrap">
<!-- 头部输入框 -->
<MyHeader :addTodo="addTodo"/>
<!-- 中间待办事项 -->
<List
:todos="todos"
:checkTodo="checkTodo"
:deleteTodo="deleteTodo"
/>
<!-- 尾部显示信息 -->
<MyFooter
:todos="todos"
:checkAllTodo="checkAllTodo"
:clearAllDoneTodo="clearAllDoneTodo"
/>
</div>
</div>
</div>
</template> <script>
import MyHeader from "@/components/MyHeader";
import List from "@/components/List";
import MyFooter from '@/components/MyFooter'; export default {
name: "App",
components:{
List,
MyFooter,
MyHeader
},
data() {
return {
// 初始化代办事项
todos: [
{id: '001', title: '吃饭', done: false},
{id: '002', title: "睡觉", done: true},
{id: '003', title: '打代码', done: false}
]
}
},
methods:{
//添加的todo
addTodo(todo){
console.log('我是app组件,我收到了数据');
// unshift添加到数组的开头
this.todos.unshift(todo);
},
// 改变todos中todo done的状态
checkTodo(id){
const todo = this.todos.find(todo => todo.id === id);
todo.done = !todo.done;
},
// 删除指定id的todo
deleteTodo(id){
this.todos = this.todos.filter(todo => todo.id !== id);
},
// 将所有的todo done改为 传入的值 true / false
checkAllTodo(done){
this.todos.forEach(todo => todo.done = done);
},
// 将所有完成的事项删除并且重新更新代办数组
clearAllDoneTodo(){
this.todos = this.todos.filter(todo => !todo.done)
}
}
}
</script> <style>
/*base*/
body {
background: #fff;
} .btn {
display: inline-block;
padding: 4px 12px;
margin-bottom: 0;
font-size: 14px;
line-height: 20px;
text-align: center;
vertical-align: middle;
cursor: pointer;
box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05);
border-radius: 4px;
} .btn-danger {
color: #fff;
background-color: #da4f49;
border: 1px solid #bd362f;
} .btn-danger:hover {
color: #fff;
background-color: #bd362f;
} .btn:focus {
outline: none;
} .todo-container {
width: 600px;
margin: 0 auto;
}
.todo-container .todo-wrap {
padding: 10px;
border: 1px solid #ddd;
border-radius: 5px;
} </style>
MyHeader.vue
<template>
<div class="todo-header">
<input type="text" placeholder="请输入你的任务名称,按回车键确认" v-model="title" @keyup.enter="add"/>
</div>
</template> <script>
import { nanoid } from 'nanoid';
export default {
// 注意不管是你写的data也还还是methods也好,甚至是computed计算属性也好都会出现在组件事例对象vc身上
// 属性值不能重名
name: "MyHeader",
data(){
return {
title: ''
}
},
methods:{
add(){
//将用户的输入包装成一个todo对象
console.log(this.title)
if(!this.title.trim()) {
alert('代办事项不能为空')
return; //输入的代办事项为空则不走下面流程
}
const todoObj = {
id: nanoid(),
title: this.title,
done:false
}
// console.log(todoObj);
this.addTodo(todoObj)
this.title = '';
}
},
props:['addTodo'],
}
</script> <style scoped>
/*header*/
.todo-header input {
width: 560px;
height: 28px;
font-size: 14px;
border: 1px solid #ccc;
border-radius: 4px;
padding: 4px 7px;
} .todo-header input:focus {
outline: none;
border-color: rgba(82, 168, 236, 0.8);
box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(82, 168, 236, 0.6);
}
</style>
List.vue
<template>
<ul class="todo-main">
<Item
v-for="todoObj in todos"
:key="todoObj.id"
:todo="todoObj"
:checkTodo="checkTodo"
:deleteTodo="deleteTodo"
/>
</ul>
</template> <script>
import Item from "@/components/Item"; export default {
name: "List",
components: {
Item,
},
props:['todos', 'checkTodo', 'deleteTodo']
}
</script> <style scoped>
/*main*/
.todo-main {
margin-left: 0;
border: 1px solid #ddd;
border-radius: 2px;
padding: 0px;
} .todo-empty {
height: 40px;
line-height: 40px;
border: 1px solid #ddd;
border-radius: 2px;
padding-left: 5px;
margin-top: 10px;
}
</style>
Item.vue
<template>
<li>
<label>
<!-- 这里勾选和取消勾选可以使用change和click作为事件处理-->
<input type="checkbox" :checked="todo.done" @change="handleCheck(todo.id)"/>
<!--v-model数据的双向绑定,checkbox使用v-model来双向绑定其是否被勾选,也可以实现效果但不推荐(因为其实修改了props中的数据)-->
<!--这里修改了从List修改过来的props,这里的不允许改是浅层次,就是如果props是一个对象则这个修改这个对象的某一个属性vue是放行的-->
<!-- <input type="checkbox" v-model="todo.done"/>-->
<span>{{ todo.title }}</span>
</label>
<button class="btn btn-danger" @click="handleDelete(todo.id)">删除</button>
</li>
</template> <script>
export default {
name: "Item",
//声明接收todo
props: ['todo', 'checkTodo', 'deleteTodo'],
methods:{
handleCheck(id){
this.checkTodo(id);
},
handleDelete(id){
if(confirm(`确定删除编号为${id}的todo吗`)){
// console.log(id);
this.deleteTodo(id);
}
}
}
}
</script> <style scoped>
/*item*/
li {
list-style: none;
height: 36px;
line-height: 36px;
padding: 0 5px;
border-bottom: 1px solid #ddd;
} li label {
float: left;
cursor: pointer;
} li label li input {
vertical-align: middle;
margin-right: 6px;
position: relative;
top: -1px;
} li button {
float: right;
display: none;
margin-top: 3px;
} li:before {
content: initial;
} li:last-child {
border-bottom: none;
} li:hover{
background: #ddd;
} li:hover button{
display: block;
}
</style>
MyFooter.vue
<template>
<!--隐式类型转换-->
<div class="todo-footer" v-show="total">
<label>
<!--这里也可用v-model来替代,此时不需要计算属性了-->
<!-- <input type="checkbox" :checked="isAll" @change="checkAll"/>-->
<input type="checkbox" v-model="isAll"/>
</label>
<span>
<span>已完成{{ doneTotal }}</span> / 全部{{total}}
</span>
<button class="btn btn-danger" @click="clearAll">清除已完成任务</button>
</div>
</template>
<script>
export default {
name: "MyFooter",
props: ['todos', 'checkAllTodo', 'clearAllDoneTodo'],
computed:{
total(){
return this.todos.length;
},
doneTotal(){
return this.todos.reduce((todoTotal, todo) => {
// 隐式类型转换
return todoTotal + todo.done;
}, 0);
// return this.todos.filter(todo => todo.done).length;
},
isAll:{
get(){
return this.total === this.doneTotal && this.doneTotal > 0; //计算属性可以通过其他的计算属性接着进行计算得到结果
},
set(value){
//value注意要么为true,要么为false,因为你是把它应用在了checkbox上
this.checkAllTodo(value);
}
}
},
methods:{
// checkAll(e){
// // console.log(e.target.checked); //判断这个checkbox到底是不是全选 true全选 false全不选
// this.checkAllTodo(e.target.checked);
// }
clearAll(){
this.clearAllDoneTodo();
}
}
}
</script> <style scoped>
/*footer*/
.todo-footer {
height: 40px;
line-height: 40px;
padding-left: 6px;
margin-top: 5px;
} .todo-footer label {
display: inline-block;
margin-right: 20px;
cursor: pointer;
} .todo-footer label input {
position: relative;
top: -1px;
vertical-align: middle;
margin-right: 5px;
} .todo-footer button {
float: right;
margin-top: 5px;
}
</style>

Tips:

  • find: array.find(function(currentValue, index, arr),thisValue), find() 方法返回通过测试(函数内判断)的数组的第一个元素的值。

  • filter: array.filter(function(currentValue,index,arr), thisValue), filter() 方法创建一个新的数组,新数组中的元素是通过检查指定数组中符合条件的所有元素。

  • forEach: array.forEach(function(currentValue, index, arr), thisValue), forEach() 方法用于调用数组的每个元素,并将元素传递给回调函数。

  • trim: string.trim(), 用来去除字符串的头尾空格, trim() 方法不会改变原始字符串。

  • reduce: array.reduce(function(total, currentValue, currentIndex, arr), initialValue), educe() 方法接收一个函数作为累加器,数组中的每个值(从左到右)开始缩减,最终计算为一个值。

  • <label>: 表示用户界面某个元素的说明, 通常与<input>关联, 将一个 <label> 和一个 <input> 元素匹配在一起,你需要给 <input> 一个 id 属性。而 <label> 需要一个 for 属性,其值和 <input>id 一样。另外,你也可以将 <input> 直接放在 <label> 里,此时则不需要 forid 属性,因为关联已隐含存在:

    <label>Do you like peas?
    <input type="checkbox" name="peas">
    </label>

十 webStorage

  1. 存储内容大小一般支持5MB左右(不同浏览器可能还不一样)

  2. 浏览器端通过 Window.sessionStorage 和 Window.localStorage 属性来实现本地存储机制。

  3. 相关API:

    1. xxxxxStorage.setItem('key', 'value');

      该方法接受一个键和值作为参数,会把键值对添加到存储中,如果键名存在,则更新其对应的值。

    2. xxxxxStorage.getItem('person');

      ​ 该方法接受一个键名作为参数,返回键名对应的值。

    3. xxxxxStorage.removeItem('key');

      ​ 该方法接受一个键名作为参数,并把该键名从存储中删除。

    4. xxxxxStorage.clear()

      ​ 该方法会清空存储中的所有数据。

  4. 备注:

    1. SessionStorage存储的内容会随着浏览器窗口关闭而消失。
    2. LocalStorage存储的内容,需要手动清除才会消失。
    3. xxxxxStorage.getItem(xxx)如果xxx对应的value获取不到,那么getItem的返回值是null。
    4. JSON.parse(null)的结果依然是null。

十一 组件的自定义事件

  1. 一种组件间通信的方式,适用于:子组件 ===> 父组件

  2. 使用场景:A是父组件,B是子组件,B想给A传数据,那么就要在A中给B绑定自定义事件(事件的回调在A中)。

  3. 绑定自定义事件:

    1. 第一种方式,在父组件中:<Demo @atguigu="test"/><Demo v-on:atguigu="test"/>

    2. 第二种方式,在父组件中:

      <Demo ref="demo"/>
      ......
      mounted(){
      this.$refs.xxx.$on('atguigu',this.test)
      }
    3. 若想让自定义事件只能触发一次,可以使用once修饰符,或$once方法。

  4. 触发自定义事件:this.$emit('atguigu',数据)

  5. 解绑自定义事件this.$off('atguigu')

  6. 组件上也可以绑定原生DOM事件,需要使用native修饰符。

  7. 注意:通过this.$refs.xxx.$on('atguigu',回调)绑定自定义事件时,回调要么配置在methods中,要么用箭头函数,否则this指向会出问题!

十二 全局事件总线(GlobalEventBus)

  1. 一种组件间通信的方式,适用于任意组件间通信。

  2. 安装全局事件总线:

    new Vue({
    ......
    beforeCreate() {
    Vue.prototype.$bus = this //安装全局事件总线,$bus就是当前应用的vm
    },
    ......
    })
  3. 使用事件总线:

    1. 接收数据:A组件想接收数据,则在A组件中给$bus绑定自定义事件,事件的回调留在A组件自身。

      methods(){
      demo(data){......}
      }
      ............................................................. mounted() {
      this.$bus.$on('xxxx',this.demo)
      }
    2. 提供数据:this.$bus.$emit('xxxx',数据)

  4. 最好在beforeDestroy钩子中,用$off去解绑当前组件所用到的事件。

Code

main.js
//引入Vue
import Vue from "vue";
//引入App
import App from './App'; //关闭Vue的生产提示
Vue.config.productionTip = false; new Vue({
el: '#app',
render: h => h(App),
beforeCreate() {
//事件总线名字叫bus 可以通过 this.$bus来调用
Vue.prototype.$bus = this;
}
});
App.vue
<template>
<div id="root">
<div class="todo-container">
<div class="todo-wrap">
<!-- addTodo 为自定义事件 Myeader组件可以通过this.$emit("addTodo",todo)-->
<MyHeader @addTodo="addTodo"/>
<List
:todos="todos"
/>
<!--
checkAllTodo为自定义事件,其子组件可以通过this.$emit("checkAllTodo",done)
clearAllDoneTodo为自定义事件, 其子组件可以通过this.$emit("clearAllDoneTodo")
-->
<MyFooter
:todos="todos"
@checkAllTodo="checkAllTodo"
@clearAllDoneTodo="clearAllDoneTodo"
/>
</div>
</div>
</div>
</template> <script>
import MyHeader from "@/components/MyHeader";
import List from "@/components/List";
import MyFooter from '@/components/MyFooter';
export default {
name: "App",
components:{
List,
MyFooter,
MyHeader
},
data() {
return {
// todos: [
// {id: '001', title: '吃饭', done: false},
// {id: '002', title: "睡觉", done: true},
// {id: '003', title: '打代码', done: false}
// ]
// 从localStorage里查找todos数据
todos:JSON.parse(localStorage.getItem('todos')) || []
}
},
methods:{
//添加的todo
addTodo(todo){
console.log('我是app组件,我收到了数据');
this.todos.unshift(todo);
},
checkTodo(id){
const todo = this.todos.find(todo => todo.id === id);
todo.done = !todo.done;
},
deleteTodo(id){
this.todos = this.todos.filter(todo => todo.id !== id);
},
checkAllTodo(done){
this.todos.forEach(todo => todo.done = done);
},
clearAllDoneTodo(){
this.todos = this.todos.filter(todo => !todo.done)
}
},
watch:{
//深度监视
todos:{
deep: true, //深度监视当我监视数组中的对象的某个属性的变化它也会产生反应
handler(newValue) {
//本地存储存的是key和value都是字符串
//数据存放在本地存储中
localStorage.setItem("todos", JSON.stringify(newValue))
}
},
},
//已挂在绑定事件总线
mounted() {
this.$bus.$on('checkTodo', this.checkTodo);
this.$bus.$on('deleteTodo', this.deleteTodo);
},
//被卸载注意解绑
beforeMount() {
this.$bus.$off('checkTodo');
this.$bus.$off('deleteTodo');
}
}
</script> <style>
/*base*/
body {
background: #fff;
} .btn {
display: inline-block;
padding: 4px 12px;
margin-bottom: 0;
font-size: 14px;
line-height: 20px;
text-align: center;
vertical-align: middle;
cursor: pointer;
box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05);
border-radius: 4px;
} .btn-danger {
color: #fff;
background-color: #da4f49;
border: 1px solid #bd362f;
} .btn-danger:hover {
color: #fff;
background-color: #bd362f;
} .btn:focus {
outline: none;
} .todo-container {
width: 600px;
margin: 0 auto;
}
.todo-container .todo-wrap {
padding: 10px;
border: 1px solid #ddd;
border-radius: 5px;
}
</style>
MyHeader.vue
<template>
<div class="todo-header">
<input type="text" placeholder="请输入你的任务名称,按回车键确认" v-model="title" @keyup.enter="add"/>
</div>
</template> <script>
import { nanoid } from 'nanoid';
export default {
//注意不管是你写的data也还还是methods也好,甚至是computed计算属性也好都会出现在组件事例对象vc身上
//属性值不能重名
name: "MyHeader",
data(){
return {
title: ''
}
},
methods:{
add(){
//将用户的输入包装成一个todo对象
console.log(this.title)
if(!this.title.trim()) {
alert('代办事项不能为空')
return; //输入的代办事项为空则不走下面流程
}
const todoObj = {
id: nanoid(),
title: this.title,
done:false
}
// console.log(todoObj);
// this.addTodo(todoObj);
// 采用自定义事件来修改
this.$emit('addTodo',todoObj);
this.title = '';
}
},
}
</script> <style scoped>
/*header*/
.todo-header input {
width: 560px;
height: 28px;
font-size: 14px;
border: 1px solid #ccc;
border-radius: 4px;
padding: 4px 7px;
} .todo-header input:focus {
outline: none;
border-color: rgba(82, 168, 236, 0.8);
box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(82, 168, 236, 0.6);
}
</style>
List.vue
<template>
<ul class="todo-main">
<Item
v-for="todoObj in todos"
:key="todoObj.id"
:todo="todoObj"
/>
</ul>
</template> <script>
import Item from "@/components/Item"; export default {
name: "List",
components: {
Item,
},
props:['todos']
}
</script> <style scoped>
/*main*/
.todo-main {
margin-left: 0;
border: 1px solid #ddd;
border-radius: 2px;
padding: 0px;
} .todo-empty {
height: 40px;
line-height: 40px;
border: 1px solid #ddd;
border-radius: 2px;
padding-left: 5px;
margin-top: 10px;
}
</style>
Item.vue
<template>
<li>
<label>
<!--这里勾选和取消勾选可以使用change和click作为事件处理-->
<input type="checkbox" :checked="todo.done" @change="handleCheck(todo.id)"/>
<!--v-model数据的双向绑定,checkbox使用v-model来双向绑定其是否被勾选,也可以实现效果但不推荐(因为其实修改了props中的数据)-->
<!--这里修改了从List修改过来的props,这里的不允许改是浅层次,就是如果props是一个对象则这个修改这个对象的某一个属性vue是放行的-->
<!-- <input type="checkbox" v-model="todo.done"/>-->
<span>{{ todo.title }}</span>
</label>
<button class="btn btn-danger" @click="handleDelete(todo.id)">删除</button>
</li>
</template> <script>
export default {
name: "Item",
//声明接收todo
props: ['todo'],
methods:{
handleCheck(id){
//事件总线
this.$bus.$emit('checkTodo',id);
},
handleDelete(id){
if(confirm(`确定删除编号为${id}的todo吗`)){
// console.log(id);
//事件总线
this.$bus.$emit('deleteTodo',id);
}
}
}
}
</script> <style scoped>
/*item*/
li {
list-style: none;
height: 36px;
line-height: 36px;
padding: 0 5px;
border-bottom: 1px solid #ddd;
} li label {
float: left;
cursor: pointer;
} li label li input {
vertical-align: middle;
margin-right: 6px;
position: relative;
top: -1px;
} li button {
float: right;
display: none;
margin-top: 3px;
} li:before {
content: initial;
} li:last-child {
border-bottom: none;
} li:hover{
background: #ddd;
} li:hover button{
display: block;
}
</style>
MyFooter.vue
<template>
<!--隐式类型转换-->
<div class="todo-footer" v-show="total">
<label>
<!--这里也可用v-model来替代,此时不需要计算属性了-->
<!-- <input type="checkbox" :checked="isAll" @change="checkAll"/>-->
<input type="checkbox" v-model="isAll"/>
</label>
<span>
<span>已完成{{ doneTotal }}</span> / 全部{{total}}
</span>
<button class="btn btn-danger" @click="clearAll">清除已完成任务</button>
</div>
</template>
<script>
export default {
name: "MyFooter",
props: ['todos'],
computed:{
total(){
return this.todos.length;
},
doneTotal(){
return this.todos.reduce((todoTotal, todo) => {
//隐士类型转换
return todoTotal + todo.done;
}, 0);
// return this.todos.filter(todo => todo.done).length;
},
isAll:{
get(){
return this.total === this.doneTotal && this.doneTotal > 0; //计算属性可以通过其他的计算属性接着进行计算得到结果
},
set(value){
//value注意要么为true,要么为false,因为你是把它应用在了checkbox上
//this.checkAllTodo(value);
//采用自定义事件来修改
this.$emit('checkAllTodo', value);
}
}
},
methods:{
// checkAll(e){
// // console.log(e.target.checked); //判断这个checkbox到底是不是全选 true全选 false全不选
// this.checkAllTodo(e.target.checked);
// }
clearAll(){
// this.clearAllDoneTodo();
//修改为自定义事件
this.$emit('clearAllDoneTodo');
}
}
}
</script> <style scoped>
/*footer*/
.todo-footer {
height: 40px;
line-height: 40px;
padding-left: 6px;
margin-top: 5px;
} .todo-footer label {
display: inline-block;
margin-right: 20px;
cursor: pointer;
} .todo-footer label input {
position: relative;
top: -1px;
vertical-align: middle;
margin-right: 5px;
} .todo-footer button {
float: right;
margin-top: 5px;
}
</style>

十三 消息订阅与发布(pubsub)

  1. 一种组件间通信的方式,适用于任意组件间通信。

  2. 使用步骤:

    1. 安装pubsub:npm i pubsub-js

    2. 引入: import pubsub from 'pubsub-js'

    3. 接收数据:A组件想接收数据,则在A组件中订阅消息,订阅的回调留在A组件自身。

      methods(){
      demo(data){......}
      }
      ......
      mounted() {
      this.pid = pubsub.subscribe('xxx',this.demo) //订阅消息
      }
    4. 提供数据:pubsub.publish('xxx',数据)

    5. 最好在beforeDestroy钩子中,用PubSub.unsubscribe(pid)去取消订阅。

十四 nextTick

  1. 语法:this.$nextTick(回调函数)
  2. 作用:在下一次 DOM 更新结束后执行其指定的回调。
  3. 什么时候用:当改变数据后,要基于更新后的新DOM进行某些操作时,要在nextTick所指定的回调函数中执行。

Code:

App.vue中关于updateTodo的关键代码
methods中:
updateTodo(id, title){
this.todos.forEach(todo => {
if(todo.id === id) todo.title = title;
})
},
//已挂在绑定事件总线
mounted() {
this.$bus.$on('updateTodo', this.updateTodo);
}
//被卸载注意解绑
beforeMount() {
this.$bus.$off('updateTodo');
}
Item.vue
<template>
<li>
<label>
<!--这里勾选和取消勾选可以使用change和click作为事件处理-->
<input type="checkbox" :checked="todo.done" @change="handleCheck(todo.id)"/>
<!--v-model数据的双向绑定,checkbox使用v-model来双向绑定其是否被勾选,也可以实现效果但不推荐(因为其实修改了props中的数据)-->
<!--这里修改了从List修改过来的props,这里的不允许改是浅层次,就是如果props是一个对象则这个修改这个对象的某一个属性vue是放行的-->
<!-- <input type="checkbox" v-model="todo.done"/>-->
<!-- 下面标签span和input只能存在一个,编辑的时候存在input,编辑完成存在span-->
<span v-show="!todo.isEdit">{{ todo.title }}</span>
<input
type="text"
:value="todo.title"
v-show="todo.isEdit"
@blur="handleBlur(todo, $event)"
ref="inputTitle"
/>
</label>
<button class="btn btn-danger" @click="handleDelete(todo.id)">删除</button>
<button v-show="!todo.isEdit" class="btn btn-edit" @click="handleEdit(todo)">编辑</button>
</li>
</template> <script>
import pubsub from "pubsub-js";
export default {
name: "Item",
//声明接收todo
props: ['todo'],
methods:{
handleCheck(id){
//事件总线
this.$bus.$emit('checkTodo',id);
},
handleDelete(id){
if(confirm(`确定删除编号为${id}的todo吗`)){
// console.log(id);
//事件总线
// this.$bus.$emit('deleteTodo',id);
//消息订阅改写
pubsub.publish('deleteTodo', id);
}
},
//编辑
handleEdit(todo){
// todo.isEdit = true; //注意这里添加的数据并不是响应式的 一定清楚
if(Object.prototype.hasOwnProperty.call(todo, 'isEdit')){
todo.isEdit = true;
}else{
this.$set(todo, 'isEdit', true); //保证初次加入的时候存在响应式的数据
}
//自动获取焦点
//this.$refs.inputTitle.focus(); //此时你这行代码执行了,但是注意vue并没有重新解析模版(input并没有出现在页面上,dom节点并没有被更新),它一定要等这个回调函数执行完之后才会去重新渲染模版
//使用nextTick来解决
this.$nextTick(() => {
//这里的回调函数注意是在dom节点被更新之后才会运行的
this.$refs.inputTitle.focus();
})
console.log(todo);
},
//失去焦点回调
handleBlur(todo, e){
todo.isEdit = false; //注意我在这里确保你身上一定存在isEdit属性
if(!e.target.value.trim()) {
alert('输入不能为空');
return;
}
this.$bus.$emit('updateTodo', todo.id, e.target.value);
}
}
}
</script> <style scoped>
/*item*/
li {
list-style: none;
height: 36px;
line-height: 36px;
padding: 0 5px;
border-bottom: 1px solid #ddd;
} li label {
float: left;
cursor: pointer;
} li label li input {
vertical-align: middle;
margin-right: 6px;
position: relative;
top: -1px;
} li button {
float: right;
display: none;
margin-top: 3px;
} li:before {
content: initial;
} li:last-child {
border-bottom: none;
} li:hover{
background: #ddd;
} li:hover button{
display: block;
}
</style>

Tips:

  • Object.prototype.hasOwnProperty.call(todo, 'isEdit'), 可以查询todo对象中是否有isEdit这个属性, 如果有就返回true, 没有就返回false.
  • this.$set(todo, 'isEdit', true); 此行代码可以给todo对象中增加一个isEdit属性, 并设置其初始值为true, 并且设置其值为响应式, 当其发生改变时vue就会更新.

十五 Vue封装的过度与动画

  1. 作用:在插入、更新或移除 DOM元素时,在合适的时候给元素添加样式类名。

  2. 图示:

  3. 写法:

    1. 准备好样式:

      • 元素进入的样式:

        1. v-enter:进入的起点
        2. v-enter-active:进入过程中
        3. v-enter-to:进入的终点
      • 元素离开的样式:
        1. v-leave:离开的起点
        2. v-leave-active:离开过程中
        3. v-leave-to:离开的终点
    2. 使用<transition>包裹要过度的元素,并配置name属性,注意如果配置了appear属性的话就代表一开始挂载真实dom的时候就开启动画的效果:

      <transition name="hello" appear>
      <h1 v-show="isShow">你好啊!</h1>
      </transition>
    3. 备注:若有多个元素需要过度,则需要使用:<transition-group>,且每个元素都要指定key值。

Code:

List.vue

<template>
<ul class="todo-main">
<!--appear代表一开始就会出现动画默认为 :appear='true'-->
<transition-group name="todo" appear>
<Item
v-for="todoObj in todos"
:key="todoObj.id"
:todo="todoObj"
/>
</transition-group>
</ul>
</template> <script>
import Item from "@/components/Item"; export default {
name: "List",
components: {
Item,
},
props:['todos']
}
</script> <style scoped>
/*main*/
.todo-main {
margin-left: 0;
border: 1px solid #ddd;
border-radius: 2px;
padding: 0px;
} .todo-empty {
height: 40px;
line-height: 40px;
border: 1px solid #ddd;
border-radius: 2px;
padding-left: 5px;
margin-top: 10px;
} .todo-enter-active{
animation: anim linear 0.5s;
} .todo-leave-active{
animation: anim linear 0.5s reverse;
} @keyframes anim {
from {
transform: translateX(-100%);
}
to{
transform: translateX(0px);
}
}
</style>

十六 vue脚手架配置代理

方法一

​ 在vue.config.js中添加如下配置:

devServer:{
proxy:"http://localhost:5000"
}

说明:

  1. 优点:配置简单,请求资源时直接发给前端(8080)即可。
  2. 缺点:不能配置多个代理,不能灵活的控制请求是否走代理。
  3. 工作方式:若按照上述配置代理,当请求了前端不存在的资源时,那么该请求会转发给服务器 (优先匹配前端资源)

方法二

​ 编写vue.config.js配置具体代理规则:

module.exports = {
devServer: {
proxy: {
'/api1': {// 匹配所有以 '/api1'开头的请求路径
target: 'http://localhost:5000',// 代理目标的基础路径
changeOrigin: true,
pathRewrite: {'^/api1': ''}
},
'/api2': {// 匹配所有以 '/api2'开头的请求路径
target: 'http://localhost:5001',// 代理目标的基础路径
changeOrigin: true,
pathRewrite: {'^/api2': ''}
}
}
}
}
/*
changeOrigin设置为true时,服务器收到的请求头中的host为:localhost:5000
changeOrigin设置为false时,服务器收到的请求头中的host为:localhost:8080
changeOrigin默认值为true
*/

说明:

  1. 优点:可以配置多个代理,且可以灵活的控制请求是否走代理。
  2. 缺点:配置略微繁琐,请求资源时必须加前缀。

Tips:

  • 现在有这个一个List, 其中所需要的数据由ajax请求得到, 数据保存在其组件的data中, 想要优雅的展示其加载过程, 可以在data内部加一个isLoading的属性, 然后使用v-show来动态展示结果信息还是Loading过程中.
  • 同样, 可以根据此思路做一个第一次加载的页面, 当是第一次加载时显示欢迎词, 将isFirst存入data中.
  • 至于数据的替换, 可以使用结构函数, 例如: this.info = { ...this.info, ...dataObj };, 其中info为存放的数据, dataObj为请求得到的新数据.

使用vueResource插件来发送请求

  1. 首先在main.js中声明使用vueResource

    Vue.use(vueResource); //使用vueResource插件来发送请求
  2. 在组件中使用vueResource

    通过this.$http.get(url)来使用vueResource, 返回的是一个Promise对象:

    this.$http.get(`https://api.github.com/search/users?q=${this.keyword}`)
    .then(res => {
    console.log(res.data.items);
    this.$bus.$emit("updateListData", {
    isLoading: false,
    errMsg: '',
    users: res.data.items
    });
    })
    .catch(e => {
    console.log(`请求失败:${e.message}`)
    this.$bus.$emit("updateListData", {
    isLoading: false,
    errMsg: e.message,
    users: []
    });
    });

十七 插槽

  1. 作用:让父组件可以向子组件指定位置插入html结构,也是一种组件间通信的方式,适用于 父组件 ===> 子组件

  2. 分类:默认插槽、具名插槽、作用域插槽

  3. 使用方式:

    1. 默认插槽:

      父组件中:
      <Category>
      <div>html结构1</div>
      </Category>
      子组件中:
      <template>
      <div>
      <!-- 定义插槽 -->
      <slot>插槽默认内容...</slot>
      </div>
      </template>
    2. 具名插槽:

      父组件中:
      <Category>
      <template slot="center">
      <div>html结构1</div>
      </template> <template v-slot:footer>
      <div>html结构2</div>
      </template>
      </Category>
      子组件中:
      <template>
      <div>
      <!-- 定义插槽 -->
      <slot name="center">插槽默认内容...</slot>
      <slot name="footer">插槽默认内容...</slot>
      </div>
      </template>
    3. 作用域插槽:

      1. 理解:数据在组件的自身,但根据数据生成的结构需要组件的使用者来决定。(games数据在Category组件中,但使用数据所遍历出来的结构由App组件决定)

      2. 具体编码:

        父组件中:
        <Category>
        <template scope="scopeData">
        <!-- 生成的是ul列表 -->
        <ul>
        <li v-for="g in scopeData.games" :key="g">{{g}}</li>
        </ul>
        </template>
        </Category> <Category>
        <template slot-scope="scopeData">
        <!-- 生成的是h4标题 -->
        <h4 v-for="g in scopeData.games" :key="g">{{g}}</h4>
        </template>
        </Category>
        子组件中:
        <template>
        <div>
        <slot :games="games"></slot>
        </div>
        </template> <script>
        export default {
        name:'Category',
        props:['title'],
        //数据在子组件自身
        data() {
        return {
        games:['红色警戒','穿越火线','劲舞团','超级玛丽']
        }
        },
        }
        </script>

十八 Vuex

1.概念

​ 在Vue中实现集中式状态(数据)管理的一个Vue插件,对vue应用中多个组件的共享状态进行集中式的管理(读/写),也是一种组件间通信的方式,且适用于任意组件间通信。

2.何时使用?

​ 多个组件需要共享数据时

3.搭建vuex环境

  1. 创建文件:src/store/index.js

    //引入Vue核心库
    import Vue from 'vue'
    //引入Vuex
    import Vuex from 'vuex'
    //应用Vuex插件
    Vue.use(Vuex) //准备actions对象 —— 响应组件中用户的动作 其中调用mutations对象中的方法
    const actions = {}
    //准备mutations对象 —— 修改state中的数据
    const mutations = {}
    //准备state对象 —— 保存具体的数据
    const state = {} //创建并暴露store
    export default new Vuex.Store({
    actions,
    mutations,
    state
    })
  2. main.js中创建vm时传入store配置项

    ......
    //引入store
    import store from './store'
    ...... //创建vm
    new Vue({
    el:'#app',
    render: h => h(App),
    store
    })

4.基本使用

  1. 初始化数据、配置actions、配置mutations,操作文件store.js

    //引入Vue核心库
    import Vue from 'vue'
    //引入Vuex
    import Vuex from 'vuex'
    //引用Vuex
    Vue.use(Vuex) const actions = {
    //响应组件中加的动作
    jia(context,value){
    // console.log('actions中的jia被调用了',miniStore,value)
    context.commit('JIA',value)
    },
    } const mutations = {
    //执行加
    JIA(state,value){
    // console.log('mutations中的JIA被调用了',state,value)
    state.sum += value
    }
    } //初始化数据
    const state = {
    sum:0
    } //创建并暴露store
    export default new Vuex.Store({
    actions,
    mutations,
    state,
    })
  2. 组件中读取vuex中的数据:$store.state.sum

  3. 组件中修改vuex中的数据:$store.dispatch('action中的方法名',数据)$store.commit('mutations中的方法名',数据)

    备注:若没有网络请求或其他业务逻辑,组件中也可以越过actions,即不写dispatch,直接编写commit

5.getters的使用

  1. 概念:当state中的数据需要经过加工后再使用时,可以使用getters加工。

  2. store.js中追加getters配置

    ......
    
    const getters = {
    bigSum(state){
    return state.sum * 10
    }
    } //创建并暴露store
    export default new Vuex.Store({
    ......
    getters
    })
  3. 组件中读取数据:$store.getters.bigSum

6.四个map方法的使用

  1. mapState方法:用于帮助我们映射state中的数据为计算属性

    computed: {
    //借助mapState生成计算属性:sum、school、subject(对象写法)
    ...mapState({sum:'sum',school:'school',subject:'subject'}), //借助mapState生成计算属性:sum、school、subject(数组写法)
    ...mapState(['sum','school','subject']),
    },
  2. mapGetters方法:用于帮助我们映射getters中的数据为计算属性

    computed: {
    //借助mapGetters生成计算属性:bigSum(对象写法)
    ...mapGetters({bigSum:'bigSum'}), //借助mapGetters生成计算属性:bigSum(数组写法)
    ...mapGetters(['bigSum'])
    },
  3. mapMutations方法:用于帮助我们生成与mutations对话的方法,即:包含$store.commit(xxx)的函数

    methods:{
    //靠mapMutations生成:increment、decrement(对象形式)
    ...mapMutations({increment:'JIA',decrement:'JIAN'}), //靠mapMutations生成:JIA、JIAN(数组写法)
    ...mapMutations(['JIA','JIAN']),
    }
  4. mapActions方法: 用于帮助我们生成与actions对话的方法, 即: 包含$store.dispatch(xxx)的函数

    methods: {
    //借助mapActions生成对应方法,方法会调用dispatch去联系actions,对象写法
    ...mapActions({
    incrementIfOdd: 'incrementIfOdd',
    incrementWait: 'incrementWait',
    }),
    // 借助mapAction生成(数组写法)
    ...mapActions(['incrementWait', 'incrementIfOdd']),
    }

备注:mapActions与mapMutations使用时,若需要传递参数需要:在模板中绑定事件时传递好参数,否则参数是事件对象。

7.模块化+命名空间

  1. 目的:让代码更好维护,让多种数据分类更加明确。

  2. 修改store.js

    const countAbout = {
    namespaced:true,//开启命名空间
    state:{x:1},
    mutations: { ... },
    actions: { ... },
    getters: {
    bigSum(state){
    return state.sum * 10
    }
    }
    } const personAbout = {
    namespaced:true,//开启命名空间
    state:{ ... },
    mutations: { ... },
    actions: { ... }
    } const store = new Vuex.Store({
    modules: {
    countAbout,
    personAbout
    }
    })
  3. 开启命名空间后,组件中读取state数据:

    //方式一:自己直接读取
    this.$store.state.personAbout.list
    //方式二:借助mapState读取:
    ...mapState('countAbout',['sum','school','subject']),
  4. 开启命名空间后,组件中读取getters数据:

    //方式一:自己直接读取
    this.$store.getters['personAbout/firstPersonName']
    //方式二:借助mapGetters读取:
    ...mapGetters('countAbout',['bigSum'])
  5. 开启命名空间后,组件中调用dispatch

    //方式一:自己直接dispatch
    this.$store.dispatch('personAbout/addPersonWang',person)
    //方式二:借助mapActions:
    ...mapActions('countAbout',{incrementOdd:'jiaOdd',incrementWait:'jiaWait'})
  6. 开启命名空间后,组件中调用commit

    //方式一:自己直接commit
    this.$store.commit('personAbout/ADD_PERSON',person)
    //方式二:借助mapMutations:
    ...mapMutations('countAbout',{increment:'JIA',decrement:'JIAN'}),

十九 路由

  1. 理解: 一个路由(route)就是一组映射关系(key - value),多个路由需要路由器(router)进行管理。
  2. 前端路由:key是路径,value是组件。

1.基本使用

  1. 安装vue-router,命令:npm i vue-router

  2. 应用插件:Vue.use(VueRouter)

  3. 编写router配置项:

    //引入VueRouter
    import VueRouter from 'vue-router'
    //引入Luyou 组件
    import About from '../components/About'
    import Home from '../components/Home' //创建router实例对象,去管理一组一组的路由规则
    const router = new VueRouter({
    routes:[
    {
    path:'/about',
    component:About
    },
    {
    path:'/home',
    component:Home
    }
    ]
    }) //暴露router
    export default router
  4. 实现切换(active-class可配置高亮样式)

    <router-link active-class="active" to="/about">About</router-link>
  5. 指定展示位置

    <router-view></router-view>

2.几个注意点

  1. 路由组件通常存放在pages文件夹,一般组件通常存放在components文件夹。
  2. 通过切换,“隐藏”了的路由组件,默认是被销毁掉的,需要的时候再去挂载。
  3. 每个组件都有自己的$route属性,里面存储着自己的路由信息。
  4. 整个应用只有一个router,可以通过组件的$router属性获取到。

3.多级路由(多级路由)

  1. 配置路由规则,使用children配置项:

    routes:[
    {
    path:'/about',
    component:About,
    },
    {
    path:'/home',
    component:Home,
    children:[ //通过children配置子级路由
    {
    path:'news', //此处一定不要写:/news
    component:News
    },
    {
    path:'message',//此处一定不要写:/message
    component:Message
    }
    ]
    }
    ]
  2. 跳转(要写完整路径):

    <router-link to="/home/news">News</router-link>

4.路由的query参数

  1. 传递参数

    <!-- 跳转并携带query参数,to的字符串写法 -->
    <router-link :to="/home/message/detail?id=666&title=你好">跳转</router-link> <!-- 跳转并携带query参数,to的对象写法 -->
    <router-link
    :to="{
    path:'/home/message/detail',
    query:{
    id:666,
    title:'你好'
    }
    }"
    >跳转</router-link>
  2. 接收参数:

    $route.query.id
    $route.query.title

5.命名路由

  1. 作用:可以简化路由的跳转。

  2. 如何使用

    1. 给路由命名:

      {
      path:'/demo',
      component:Demo,
      children:[
      {
      path:'test',
      component:Test,
      children:[
      {
      name:'hello' //给路由命名
      path:'welcome',
      component:Hello,
      }
      ]
      }
      ]
      }
    2. 简化跳转:

      <!--简化前,需要写完整的路径 -->
      <router-link to="/demo/test/welcome">跳转</router-link> <!--简化后,直接通过名字跳转 -->
      <router-link :to="{name:'hello'}">跳转</router-link> <!--简化写法配合传递参数 -->
      <router-link
      :to="{
      name:'hello',
      query:{
      id:666,
      title:'你好'
      }
      }"
      >跳转</router-link>

6.路由的params参数

  1. 配置路由,声明接收params参数

    {
    path:'/home',
    component:Home,
    children:[
    {
    path:'news',
    component:News
    },
    {
    component:Message,
    children:[
    {
    name:'xiangqing',
    path:'detail/:id/:title', //使用占位符声明接收params参数
    component:Detail
    }
    ]
    }
    ]
    }
  2. 传递参数

    <!-- 跳转并携带params参数,to的字符串写法 -->
    <router-link :to="/home/message/detail/666/你好">跳转</router-link> <!-- 跳转并携带params参数,to的对象写法 -->
    <router-link
    :to="{
    name:'xiangqing',
    params:{
    id:666,
    title:'你好'
    }
    }"
    >跳转</router-link>

    特别注意:路由携带params参数时,若使用to的对象写法,则不能使用path配置项,必须使用name配置!

  3. 接收参数:

    $route.params.id
    $route.params.title

7.路由的props配置

​ 作用:让路由组件更方便的收到参数

{
name:'xiangqing',
path:'detail/:id',
component:Detail, //第一种写法:props值为对象,该对象中所有的key-value的组合最终都会通过props传给Detail组件
// props:{a:900} //第二种写法:props值为布尔值,布尔值为true,则把路由收到的所有params参数通过props传给Detail组件
// props:true //第三种写法:props值为函数,该函数返回的对象中每一组key-value都会通过props传给Detail组件
props(route){
return {
id:route.query.id,
title:route.query.title
}
}
props({ query: { id } }){
return {
id
}
}
}

8.<router-link>的replace属性

  1. 作用:控制路由跳转时操作浏览器历史记录的模式
  2. 浏览器的历史记录有两种写入方式:分别为pushreplacepush是追加历史记录,replace是替换当前记录。路由跳转时候默认为push
  3. 如何开启replace模式:<router-link replace .......>News</router-link>

9.编程式路由导航

  1. 作用:不借助<router-link> 实现路由跳转,让路由跳转更加灵活

  2. 具体编码:

    //$router的两个API
    this.$router.push({
    name:'xiangqing',
    params:{
    id:xxx,
    title:xxx
    }
    }) this.$router.replace({
    name:'xiangqing',
    params:{
    id:xxx,
    title:xxx
    }
    })
    this.$router.forward() //前进
    this.$router.back() //后退
    this.$router.go() //可前进也可后退 参数是number类型(正数为前进的步数,负数为后退的部署)

10.缓存路由组件

  1. 作用:让不展示的路由组件保持挂载,不被销毁。

  2. 具体编码:

    <keep-alive include="News">
    <router-view></router-view>
    </keep-alive>

11.两个新的生命周期钩子

  1. 作用:路由组件所独有的两个钩子,用于捕获路由组件的激活状态。

  2. 具体名字:

    1. activated路由组件被激活时触发。
    2. deactivated路由组件失活时触发。
  3. 具体编码:

    //激活(路由组件独有的两个钩子)
    activated() {
    console.log('News组件被激活');
    this.timer = setInterval(() => {
    console.log('@')
    this.opacity -= 0.01;
    if(this.opacity <= 0) this.opacity = 1;
    },16);
    },
    //失活
    deactivated() {
    console.log('News组件失活了');
    clearInterval(this.timer);
    }

12.路由守卫

  1. 作用:对路由进行权限控制

  2. 分类:全局守卫、独享守卫、组件内守卫

  3. meta: 路由元信息,可以配置是否需要校验的信息, 在router/index.js里配置

    ......
    const router = new VueRouter({
    routes:[
    {
    name: 'regard',
    path:'/about',
    component: About,
    meta:{
    title: '关于'
    }
    },
    {
    name:'homepage',
    path:'/home',
    component: Home,
    meta:{
    title: '主页'
    },
    children:[
    {
    name: 'ns',
    path: 'news',
    component: News,
    //meta:路由元信息,可以配置是否需要校验的信息
    meta:{
    isAuth: true,
    title: '新闻'
    }
    },
    {
    name:'msg',
    path: 'message',
    component: Message,
    children:[
    {
    name: 'particulars',
    path: 'detail',
    component: Detail,
    props({ query: { id, title } }){
    return {
    id,
    title
    }
    }
    }
    ],
    //meta:路由元信息,可以配置是否需要校验的信息
    meta:{
    isAuth: true,
    title: '消息'
    }
    }
    ]
    }
    ]
    });
    ......
  4. 全局守卫:写在routers外部.

    //全局前置守卫:初始化时执行、每次路由切换前执行
    router.beforeEach((to,from,next)=>{
    console.log('beforeEach',to,from)
    if(to.meta.isAuth){ //判断当前路由是否需要进行权限控制
    if(localStorage.getItem('school') === 'atguigu'){ //权限控制的具体规则
    next() //放行
    }else{
    alert('暂无权限查看')
    // next({name:'guanyu'})
    }
    }else{
    next() //放行
    }
    }) //全局后置守卫:初始化时执行、每次路由切换后执行
    router.afterEach((to,from)=>{
    console.log('afterEach',to,from)
    if(to.meta.title){
    document.title = to.meta.title //修改网页的title
    }else{
    document.title = 'vue_test'
    }
    })
  5. 独享守卫:写在routers所需路由配置项中.

    beforeEnter(to,from,next){
    console.log('beforeEnter',to,from)
    if(to.meta.isAuth){ //判断当前路由是否需要进行权限控制
    if(localStorage.getItem('school') === 'atguigu'){
    next()
    }else{
    alert('暂无权限查看')
    // next({name:'guanyu'})
    }
    }else{
    next()
    }
    }
  6. 组件内守卫: 在路由组件内配置

    // 进入守卫:通过路由规则,进入该组件时被调用
    // 注意一定是通过路由规则进入组件,普通的组件装载是不会调用的
    beforeRouteEnter (to, from, next) {
    console.log('App---beforeRouteEnter');
    console.log(from, to);
    const { isAuth } = to.meta;
    if(isAuth){
    //代表需要鉴权
    if(localStorage.getItem('school') === 'wust1') next();//类似于nodejs中间件
    else alert('无权限');
    }else{
    next();
    },
    //离开守卫:通过路由规则,离开该组件时被调用
    beforeRouteLeave (to, from, next) {
    console.log('App---beforeRouteLeave');
    console.log(from, to);
    next();
    }

13.路由器的两种工作模式

  1. 对于一个url来说,什么是hash值?—— #及其后面的内容就是hash值。

  2. hash值不会包含在 HTTP 请求中,即:hash值不会带给服务器。

  3. 默认是hash模式, 如果想修改成history模式,要在router/index.js的router里增加配置项mode

    ......
    //创建一个路由器
    const router = new VueRouter({
    //默认开启hash模式
    mode: 'history',
    routes:[
    ......
    }) export default router;
  4. hash模式:

    1. 地址中永远带着#号,不美观 。
    2. 若以后将地址通过第三方手机app分享,若app校验严格,则地址会被标记为不合法。
    3. 兼容性较好。
  5. history模式:

    1. 地址干净,美观 。
    2. 兼容性和hash模式相比略差。
    3. 应用部署上线时需要后端人员支持,解决刷新页面服务端404的问题。(可以使用node-js中的connect-history-api-fallback插件)

VUE学习笔记(李天禹老师版本)的更多相关文章

  1. Vue学习笔记-2

    前言 本文非vue教程,仅为学习vue过程中的个人理解与笔记,有说的不正确的地方欢迎指正讨论 1.computed计算属性函数中不能使用vm变量 在计算属性的函数中,不能使用Vue构造函数返回的vm变 ...

  2. vue学习笔记(九)vue-cli中的组件通信

    前言 在上一篇博客vue学习笔记(八)组件校验&通信中,我们学会了vue中组件的校验和父组件向子组件传递信息以及子组件通知父组件(父子组件通信),上一篇博客也提到那是对组件内容的刚刚开始,而本 ...

  3. vue学习笔记(十)路由

    前言 在上一篇博客vue学习笔记(九)vue-cli中的组件通信内容中,我们学习组件通信的相关内容和进行了一些组件通信的小练习,相信大家已经掌握了vue-cli中的组件通信,而本篇博客将会带你更上一层 ...

  4. Vue学习笔记-Vue.js-2.X 学习(四)===>脚手架Vue-CLI(基本工作和创建)

    (五) 脚手架Vue-CLI 一 Vue-CLI前提(nodejs和webpack) 二  Vue学习-nodejs按装配置,Node.js 就是运行在服务端的 JavaScript. 1. 去nod ...

  5. Vue学习笔记-Vue.js-2.X 学习(一)===>基本知识学习

    一  使用环境: windows 7 64位操作系统 二  IDE:VSCode/PyCharm 三  Vue.js官网: https://cn.vuejs.org/ 四  下载安装引用 方式1:直接 ...

  6. Vue学习笔记-Django REST framework3后端接口API学习

    一  使用环境 开发系统: windows 后端IDE: PyCharm 前端IDE: VSCode 数据库: msyql,navicat 编程语言: python3.7  (Windows x86- ...

  7. Vue学习笔记-vue-element-admin 按装报错再按装

    一  使用环境 开发系统: windows 后端IDE: PyCharm 前端IDE: VSCode 数据库: msyql,navicat 编程语言: python3.7  (Windows x86- ...

  8. Vue学习笔记-1

    前言 本文不是Vue.js的教程,只是一边看官网Vue的教程文档一边记录并总结学习过程中遇到的一些问题和思考的笔记. 1.vue和avalon一样,都不支持VM初始时不存在的属性 而在Angular里 ...

  9. vue 学习笔记(二)

    最近公司赶项目,一直也没时间看 vue,之前看下的都快忘得差不多了.哈哈哈,来一起回顾一下vue 学习笔记(一)后,继续向下看嘛. #表单输入绑定 基础用法 v-model 会忽略所有表单元素的 va ...

随机推荐

  1. ctf--web刷题记录 ACTF2020back up file 、极客大挑战2019php、secret file

    ACTF2020back up file backup file指的是备份文件,一般备份文件的后缀有".git" .".svn"." .swp&quo ...

  2. AOP操作-AspectJ配置文件

    AOP操作(AspectJ配置文件)(了解) (实际中大部分用注解方式) 1,创建两个类,增强类和被增强类,创建方法 2,在spring配置文件中创建两个类对象 3,在spring配置文件中配置切入点

  3. linux信号 SIGINT SIGTERM SIGKILL

    三者都是结束/终止进程运行. 1.SIGINT SIGTERM区别 前者与字符ctrl+c关联,后者没有任何控制字符关联. 前者只能结束前台进程,后者则不是. 2.SIGTERM SIGKILL的区别 ...

  4. 【重构前端知识体系之HTML】HTML5给网页音频带来的变化

    [重构前端知识体系之HTML]HTML5给网页音频带来的变化 引言 音乐播放,相信大家都很熟悉,但是早在之前的音乐播放之前,你的浏览器会问你,是否下载flash插件.然而现在,估计一些年轻的开发者都不 ...

  5. ApacheCN jQuery 译文集 20211121 更新

    创建 jQueryMobile 移动应用 零.序言 一.jQueryMobile 原型制作 二.jQuery Mobile 网站 三.分析.长表单和前端验证 四.QR 码.地理位置.谷歌地图 API ...

  6. UTF-8与UTF-8(BOM)区别

    感谢原文作者:Teingi 原文链接:https://blog.csdn.net/weixin_40449300/article/details/86567129 在我们通常使用的windows系统中 ...

  7. Android图表库hellocharts详解

    感谢大佬:https://www.cnblogs.com/huolongluo/p/5988644.html 因为项目需要搞一个折线图,按照日期显示相应的成绩,所以有了本文. 以前用过一次XCL-ch ...

  8. Linux 打包压缩、软链接、硬链接、配置镜像源

    tar命令:tar -cvf 打包文件.tar  被打包文件的路径   把文件打包成tar包,但并未被压缩: [root@Server-n93yom test]# ll total 0 -rw-r-- ...

  9. express源码分析之Router

    express作为nodejs平台下非常流行的web框架,相信大家都对其已经很熟悉了,对于express的使用这里不再多说,如有需要可以移步到www.expressjs.com自行查看express的 ...

  10. centOs编译安装php7.2支持微擎php扩展

    发现yum安装许多坑 于是只好编译安装 第一步得到镜像地址 在      https://www.php.net/downloads.php     有的地址比较慢,需要耐心等待 cd /usr/lo ...