Nuxt 三方js使用、自定义 js 挂载到 到 Vue 实例上

1、Vue.use形式

1)、创建文件 plugins -> vant.js

 

 

import Vue from 'vue'
import Vant from 'vant'

Vue.use(Vant)

2)、nuxt.config.js 内引入插件

2、Vue.prototype 形式

1)、创建文件 plugins -> methods.js

import Vue from 'vue';

Vue.prototype.$login = (options = {}) => {
    /*
    * @description:         判断是否登录
    * @use:                 this.$login()
    * @options -> hasJump   未登录是否立即跳登录页,默认 true
    */ 
    options.hasJump ?? (options.hasJump = true)
    options.token = localStorage.getItem('token') || ''

    options.hasJump && !options.token && (location.href = `/login`)

    return !!options.token
}

2)、nuxt.config.js 内引入插件

 

posted @ 2022-09-14 16:17  忙着可爱呀~  阅读(63)  评论(0)    收藏  举报