前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Vue笔记,持续更新

Vue笔记,持续更新

作者头像
用户2845596
发布2021-01-21 12:03:42
4360
发布2021-01-21 12:03:42
举报
文章被收录于专栏:劝学劝学

get element time

mounted函数里,通过this.$refdocument.querySelector都访问不了dom

vue文档里关于ref的触发时机说明:

cn.vuejs.org/v2/api/inde…

关于 ref 注册时间的重要说明:因为 ref 本身是作为渲染结果被创建的,在初始渲染的时候你不能访问它们 - 它们还不存在!$refs 也不是响应式的,因此你不应该试图用它在模板中做数据绑定。

代码语言:javascript
复制
{
    mounted() {
        // get ele via ref
        this.$refs.xxx   // fail 
        
        // get ele via document.querySelecor
        document.querySelector('.xxx')
    }
}

route update hook

vue-router@^2.2里,新增了beforeRouteUpdate事件钩子,可以用来处理当前页面路由变化时的一些变化。 当然也可以watch $route对象,来达到同样的效果

那他们有什么不同的地方呢?看下面的Demo

代码语言:javascript
复制
// 假设页面路由模式是hash,当前路径是#/path/to?state=pending
// 当按下button时,会触发当前路由参数的改变
new Vue({
    template: `
        
            Push State
        

    `,
    beforeRouteUpdate(to, form, next){
        // 此时这里this.$route还是之前的route
        console.log(this.$route.query.state) // pending
        next()
    },
    watch: {
        // $route的变化,可能跳到其他页面,也可能是其他页面跳到此页面
        // 一般我们只需要设置其他页面跳到此页面就行了。
        // 对于使用了组件的,直接使用activated钩子就行了
        $route(to, from) {
            // watch $route回调里的$route,已是更新后的$route
            console.log(this.$route.query.state)  // completed
        }
    },
    methods: {
        click() {
            this.$router.push(`/path/to?state=completed`)
        }
    }
})

vue-loader deep selector

vue-loader@~12.2.2以上才支持deep selector

而这之前,如果要支持deep selector,只能使用style global(不带scoped属性即可)

获取嵌套组件

代码语言:javascript
复制
let vm = new Vue()
// 取$route匹配的最后一个$route的instances.default
vm.$route.matched[vm.$route.matched.length - 1].instances.default
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • get element time
  • route update hook
  • vue-loader deep selector
  • 获取嵌套组件
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档