1 Star 0 Fork 5

杨长赫/Vue2-Vue3的学习笔记

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
LGPL-3.0

vue_test项目

Vue脚手架启动并运行项目的命令

npm run serve

Vue脚手架构建项目使用的命令

npm run build

笔记

ref属性

  1. 被用来给元素或子组件注册引用信息(id的替代者)

  2. 应用在html标签上获取的是真实DOM元素,应用在组件标签上是组件实例对象(vc)

  3. 使用方式: 打标识:

    <h1 ref="xxx">.....</h1><School ref="xxx"><School>
    获取: this.$refs.xxx
    

配置项props

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

<Demo name="xxx"/>

​ (2).接收数据:

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

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

//第三种方式(限制类型、限制必要性、指定默认值):
props:{
    name:{
        type:String,//类型
            required:true,//必要性
                default:'老王'//默认值
    }
}
//备注: props是只读的,Vue底层会监测你对props的修改,如果进行了修改,就会发出警告,
//若业务需求确实需要修改,那么请复制props的内容到data中一份,然后去修改data中的数据。

mixin(混入)

//功能:可以把多个组件共用的配置提取成一个混入对象
//使用方式:
//第一步定义混合,例如:
{
    data(){....},
    methods:{....}
}
//第二步使用混入,例如:
//(1).全局混入:Vue.mixin(xxx)
//(2).局部混入: mixins:["xxx"]

插件

//功能:用于增强Vue
//本质:包含install方法的一个对象,install的第一个参数是Vue,第二个以后的参数是插件使用者传递的数据。
//定义插件:
对象.install = function (vue,options) {
    //1.添加全局过滤器
    Vue.filter(....)

    //2.添加全局指令
    Vue.directive(....)

    //3.配置全局混入(合)
    Vue.mixin(....)
    
    //4.添加实例方法
    Vue.prototype.$myMethod = function(){...}
    Vue.prototype. $myProperty = xxxx
}
//使用插件:Vue.use()

scoped样式

作用:让样式在局部生效,防止冲突。写法:

<style scoped>

TodoList案例总结

  1. 组件化编码流程:
(1).拆分静态组件:组件要按照功能点拆分,命名不要与html元素冲突。
(2).实现动态组件:考虑好数据的存放位置,数据是一个组件在用,还是一些组件在用:
    1).一个组件在用:放在组件自身即可。
    2).一些组件在用:放在他们共同的父组件上(<span style=" color:red">状态提升</span>)
(3).实现交互:从绑定事件开始。
  1. props适用于: (1).父组件==>子组件通信 (2).子组件==>父组件通信(要求父先给子一个函数)

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

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

webStorage

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

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

  3. 相关API:

    1. xxxxxStorage.setItem('key',"value");--->该方法接受一个键和值作为参数,会把键值对添加到存储中,如果键名存在,则更新其对应的值。
    2. xxxStorage-getItem('person');--->该方法接受一个键名作为参数,返回键名对应的值。
    3. xxxStorage.removeItem("key");--->该方法接受一个键名作为参数,并把该键名从存储中删除。
    4. xxxStorage.clear();--->该方法会清空存储中的所有数据。
  4. 备注:

    1. SessionStorage存储的内容会随着浏览器窗口关闭而消失。
    2. LocalStorage存储的内容,需要手动清除才会消失。
    3. xxxStorage.getItem(xoxx)如果xxx对应的value获取不到,那么getltem的返回值是null。
    4. ]SON. parse(nul1)的结果依然是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.xx.$on('atguigu',回调)绑定自定义事件时,回调要么配置在methods中,要么用箭头函数,否则this指向会出问题。

全局事件总线(GldbalEventBus)

  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)
    }
    
  4. 提供数据:this.$bus.$emit('xxxx',数据)

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

消息订阅与发布(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所指定的回调函数中执行。

vue脚手架配置代理

方法一

//在vue.config.js中添加如下配置:
devServer:{
    proxy:"http://localhost : 5000"
}

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

方法二

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

//开启代理服务器(方式二)代理多台后端服务器,配置请求前缀
devServer: {
    proxy: {
        '/atguigu': {
            target: 'http://localhost:5000',
            pathRewrite: {'^/atguigu':''},//将请求路径的前缀使用正则表达式去掉(一定要记得)
            wc: true, //用于支持websocket
            changeOrigin: true //用于控制请求头中的host值
        },
        '/demo':{
            target: 'http://localhost:5001',
            pathRewrite: {'^/demo':''},//将请求路径的前缀使用正则表达式去掉(一定要记得)
            wc: true, //用于支持websocket
            changeOrigin: true //用于控制请求头中的host值
        }
    }
}

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

插槽

  1. 作用:让父组件可以向子组件指定位置插入html结构,也是一种组件间通信的方式,适用于父组件===>子组件。
  2. 分类:默认插槽、具名插槽、作用域插槽。

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

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
  1. 实现切换(active-class可配置高亮样式)

    <!--路由连接标签-->
    <router-link active-class="active" to="/about">About</router-link>
    
  2. 指定展示位置

    <!--路由组件显示标签-->
    <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
            }
        ]
    }
]
  1. 跳转(要写完整路径):
<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

编程式路由导航

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

路由缓存组件

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

  2. 具体编码:

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

两个新的生命周期钩子

  1. 作用:路由组件所独有的两个钩子用于捕获路由组件的激活状态。
  2. 具体名字:
    1. activated路由组件被激活时触发。
    2. deactivated路由组件失活时触发。

路由守卫

  1. 作用:对路由进行权限控制
  2. 分类:全局守卫、独享守卫、组件内守卫
  3. 全局守卫:
//全局前置路由守卫--->初始化的时候被调用、每次路由切换前被调用
router.beforeEach((to,from,next)=>{
    if (to.meta.isAuth) {//判断一下是否需要限权
        if (localStorage.getItem("school")!='atguigu') {
            next();//放行
        }else{
            alert("学校名不对没有权限");
        }
    }else{
        next();//放行,不然所有的路由请求都会被拦截
    }
});
//全局后置路由守卫--->初始化的时候被调用、每次路由切换前被调用
router.afterEach((to,from)=>{
    document.title=to.meta.title || "尚硅谷系统";
});

路由器的两种工作模式

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

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

  3. hash模式:

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

    1. 地址干净,美观。
    2. 兼容性和hash模式相比略差。
    3. 应用部署上线时需要后端人员支持,解决刷新页面服务端404的问题。路由器的两种工作模式
  5. 对于一个url来说,什么是hash值?——#及其后面的内容就是hash值。

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

  7. hash模式:

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

    1. 地址干净,美观。
    2. 兼容性和hash模式相比略差。
  9. 应用部署上线时需要后端人员支持,解决刷新页面服务端404的问题。

GNU LESSER GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/> Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below. 0. Additional Definitions. As used herein, "this License" refers to version 3 of the GNU Lesser General Public License, and the "GNU GPL" refers to version 3 of the GNU General Public License. "The Library" refers to a covered work governed by this License, other than an Application or a Combined Work as defined below. An "Application" is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library. A "Combined Work" is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the "Linked Version". The "Minimal Corresponding Source" for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version. The "Corresponding Application Code" for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work. 1. Exception to Section 3 of the GNU GPL. You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL. 2. Conveying Modified Versions. If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version: a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy. 3. Object Code Incorporating Material from Library Header Files. The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following: a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the object code with a copy of the GNU GPL and this license document. 4. Combined Works. You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following: a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the Combined Work with a copy of the GNU GPL and this license document. c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document. d) Do one of the following: 0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source. 1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version. e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.) 5. Combined Libraries. You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following: a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License. b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work. 6. Revised Versions of the GNU Lesser General Public License. The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation. If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library.

简介

Vue2和Vue3全家桶学习笔记上传,我的Vue老师是尚硅谷的前端讲师张天禹 展开 收起
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/yang-changhe/Vue2-Vue3.git
git@gitee.com:yang-changhe/Vue2-Vue3.git
yang-changhe
Vue2-Vue3
Vue2-Vue3的学习笔记
master

搜索帮助