Use the idea of mixin to achieve. import Taro from '@tarojs/taro'; //The mixin that needs to be logged in if you are not logged in export const needLogin = {//mixin way mounted(){ const isLogin = Taro.getStorageSync('token'); const url=getCurrentPageUrlWithArgs();//The current path with parameters if(!isLogin){ Taro.navigateTo({url:`/pages/login/index?url=${encodeURIComponent(url)}`}); } } }
Add imixn to the page that requires login
export default { name: "AddCars", mixins:[needLogin], }