mixin是一种类,在vue中就是js文件,主要的作用是作为功能模块引用
步骤:
定义mixin混入全局鉴权方法
组件onShow时检测
mixin/index.js
export default {
methods: {
checkAuth () {
if (!uni.getStorageSync('token')) {
uni.navigateTo({
url: '/packone/auth/index'
})
}
}
},
}
main.js
// 注册mixin
+ import mixins from '@/utils/mixins'
export function createApp () {
const app = createSSRApp(App);
app.component('Search', Search);
+ app.mixin(mixins);
app.use(plugins)
return {
app,
};
}
鉴权的页面中xxx/index.vue
<script>
export default {
// 页面显示执行该方法
onShow () {
this.checkAuth()
}
};
</script>