推广

如何吃透 vue-router

iseeyu3年前 (2024-02-21)推广160

image.png

“您使用的是Vue的仅运行时构建,而模板编译器不可用。要么将模板预编译为呈现函数,要么使用包含编译器的构建。”

出现这个问题就涉及到了vue的构建版本的差异。

我们的使用cli 运行项目的时候,使用的是vue的运行时版本构建的,它不支持template模板编译,需要打包的时候提前编译。而完整版包含了运行时版本和编译器,在运行的时候会把模板转换成render函数,因此体积比运行时版本大10KB左右。

针对这种情况 Vue 官方已经给出我们解决的方案(参考链接),只需要我们在vue.config.js中,将 runtimeCompiler 设置成 true 即可。

除此之外我们还可以自己去使用 render 渲染模板。

  initComponents (Vue) {
    Vue.component('router-link', {
      props: {
        to: String
      },
      // template: '<a :href="to"><slot></slot></a>'
      render (h) { // h 是一个函数用来创建虚拟dom
        return h('a', {
          attrs: {
            href: this.to // history 模式
          }
        }, [this.$slots.default])
      }
    })
  }

继续创建 route-view 组件
我们route-view 组件的内容已经存储在 routeMap 里了,所以只需要我们将其取出转换成 vDom 即可

  initComponents (Vue) {
    Vue.component('router-link', {
      props: {
        to: String
      },
      // template: '<a :href="to"><slot></slot></a>'
      render (h) { // h 是一个函数用来创建虚拟dom
        return h('a', {
          attrs: {
            href: this.to // history 模式
          }
        }, [this.$slots.default])
      }
    })

    const _this = this // 记录 当前 vuerouter 对象
    Vue.component('router-view', {
      render (h) {
        const component = _this.routeMap[_this.data.current]
        return h(component)
      }
    })
  }

这个时候我们满心以为我们的vue-router 基本功能已经完成的时候,突然想到了我们忽略了一件事儿,因为我们之前的route-link 使用的 a标签 本身的默认事件是跳转并且从服务器请求。因此需要我们给 route-link 做一下改造。

  initComponents (Vue) {
    Vue.component('router-link', {
      props: {
        to: String
      },
      // template: '<a :href="to"><slot></slot></a>'
      render (h) { // h 是一个函数用来创建虚拟dom
        return h('a', {
          attrs: {
            href: this.to // history 模式
          },
          on: {
            click: this.clickHandler
          }
        }, [this.$slots.default])
      },
      methods: {
        clickHandler (e) {
          history.pushState({/* 传递给 popstate 参数 */}, '', this.to) // 改变地址栏
          this.$router.data.current = this.to // 改变current 重新出发渲染
          e.preventDefault()
        }
      }
    })

    const _this = this // 记录 当前 vuerouter 对象
    Vue.component('router-view', {
      render (h) {
        const component = _this.routeMap[_this.data.current]
        return h(component)
      }
    })
  }

截止到现在我们的基本功能已经可以使用了,但是我们发现之前提到过的 initEvent 没有实现,他的作用是注册 popstate 方法,监听浏览器地址变化,我们点击前进后退的时候就会发现 页面没有变化。因此我们还需要实现initEvent。

  initEvent () {
    window.addEventListener('popstate', () => { // 箭头函数不改变this指向
      this.data.current = window.location.pathname
    })
  }

现在我们完善了我们的代码,奉上

let _Vue = null

export default class VueRouter {
  constructor (options) {
    this.options = options
    this.data = _Vue.observable({
      current: '/'
    })
    this.routeMap = {}
  }

  createRouteMap () {
    this.options.routes.forEach(route => {
      this.routeMap[route.path] = route.component
    })
  }

  init () {
    this.createRouteMap()
    this.initComponents(_Vue)
    this.initEvent()
  }

  initComponents (Vue) {
    Vue.component('router-link', {
      props: {
        to: String
      },
      // template: '<a :href="to"><slot></slot></a>'
      render (h) { // h 是一个函数用来创建虚拟dom
        return h('a', {
          attrs: {
            href: this.to // history 模式
          },
          on: {
            click: this.clickHandler
          }
        }, [this.$slots.default])
      },
      methods: {
        clickHandler (e) {
          history.pushState({/* 传递给 popstate 参数 */}, '', this.to) // 改变地址栏
          this.$router.data.current = this.to // 改变current 重新出发渲染
          e.preventDefault()
        }
      }
    })

    const _this = this // 记录 当前 vuerouter 对象
    Vue.component('router-view', {
      render (h) {
        const component = _this.routeMap[_this.data.current]
        return h(component)
      }
    })
  }

  initEvent () {
    window.addEventListener('popstate', () => { // 箭头函数不改变this指向
      this.data.current = window.location.pathname
    })
  }

  static install (Vue) {
    // 1. 判断当前插件是否被安装
    if (VueRouter.install.installed) {
      return
    }
    VueRouter.install.installed = true
    // 2. 把 Vue 构造函数记录到全局变量
    _Vue = Vue
    // 3. 把创建 Vue 实例的时候传入的 router 对象注入到 Vue 实例上
    _Vue.mixin({
      beforeCreate () {
        if (this.$options.router) { // 在 vue 中执行,其他组件 $options.router 不存在,保证以下代码执行一次
          _Vue.prototype.$router = this.$options.router
          this.$options.router.init()
        }
      }
    })
  }
}

运行!可用!正常!
vue-router 的 history 模式完成!

扫描二维码推送至手机访问。

版权声明:本文由西安泽虎代运营发布,如需转载请注明出处。

转载请注明出处https://www.0291.com.cn/post/57399.html

相关文章

西安企业做百度推广,一个月花三千和花三万到底差在哪?

4|最近跟几个西安的老板聊天,发现一个挺有意思的现象:同样做百度推广,有人一个月花三千块,询盘不断;有人一个月砸三万,水花都没见着。问他们区别在哪,大多数人说不清楚,只知道'钱花了,没效果'。 5| 6|百度推广这东西,说复杂也复杂,说简单也简单。核心就一件事:让你的潜在客户搜某个词的时候,你的信息...

我想当策划,大学应该选什么专业?

我想当策划,大学应该选什么专业?

学,广告,传播,公关,这些专业其实都可以。另外也可以学像汉语言文学,以及一些跟文学相关的一些专业,会对你找工作有帮助。但是,你想从事这个这份职业,除了专业的学习之外,更多的是,你要在上学的时候积极参与一些更营销策划相关的社会实践。比如说你可以做一些兼职,比如说帮别人去写稿,...

服装市场营销策略分析,“定位不慎满盘皆输”

服装市场营销策略分析,“定位不慎满盘皆输”

不同的区域需要不同的策略,而不同的营销策略需要不同的销售思路来配合。因此,在研究女装产品销售思路之前,最好先对于市场与营销策略做一次深入地了解。1.市场区域化(market segmentation):目标营销的第一步,是将整个市场拆分成若干个同性质的小市场。2.目标市场的...

晚间悦读丨你的思维方式,决定你的人生高度!

晚间悦读丨你的思维方式,决定你的人生高度!

越长大,越发现,能够决定你人生高度的,往往是你的认知层次。 你的认知在哪个层级,你的人生就处在什么状态。你认知的高度,决定了你人生的高度。 那如何才能升级方式、提升认知能力呢?可以从这三个方面做出改变。 NO. 1 改变态度 有人...

编写项目营销策划方案六大要点

编写项目营销策划方案六大要点

现在商业发展非常迅速,的需求非常的大,但是要做好营销策划是有一定难度的,我把自己做营销策划的一些体会在这里分享给朋友们。营销策划方案六大要点我们先说项目营销策划方案这个话题。大部分的营销策划人都是在企业里工作,大部分项目之初都是来源于一个想法,创始人、老板或者创始团队的一个...

【商场营销策划】如何进行商场营销策划?

【商场营销策划】如何进行商场营销策划?

商场是策划者们关注的永恒话题,那么究竟该如何作好商场营销策划呢?掌握以下这几点营销策略设计原则,至关重要!1.义利兼顾的原则。商场在获取利润的同时要充分考虑到消费者的合法利益和商场的长远利益。在作商场活动营销策略方案的时候应该将这一点作为首要的原则。对商场有利而对顾客不利的...

现在,非常期待与您的又一次邂逅

我们努力让每一部企业宣传片和抖音短视频成为商业大片