1.这是封装的跳转方法:
javascript
import store from "../stores/store";
function Router(type, url, params) {
const NoLoginPage = [
。。。。。
];
var queryString = Object.keys(params)
.map((key) => `${key}=${params[key]}`)
.join("&");
if (!NoLoginPage.includes(url)) {
if (!store.state.user_store.token) {
store.commit("user_store/setPath", url);
store.commit("user_store/setParams", params);
uni.navigateTo({
url: "/pages/login/login",
});
return;
}
}
switch (type) {
case "switchTab":
if (queryString) {
uni.switchTab({
url: `${url}?${queryString}`,
});
} else {
uni.switchTab({
url: url,
});
}
break;
case "navigateTo":
if (queryString) {
uni.navigateTo({
url: `${url}?${queryString}`,
});
} else {
uni.navigateTo({
url: url,
});
}
break;
case "redirectTo":
if (queryString) {
uni.redirectTo({
url: `${url}?${queryString}`,
});
} else {
uni.redirectTo({
url: url,
});
}
break;
case "switchTab":
if (queryString) {
uni.switchTab({
url: `${url}?${queryString}`,
});
} else {
uni.switchTab({
url: url,
});
}
break;
}
}
export default Router;
2.引入main.js函数进行使用
javascript
import Router from "./router/routerLogin";
Vue.prototype.$Router = Router;
3.组件中使用
javascript
this.$Router(
"navigateTo",
"/page_my/。。。。。",
""
);
4.非常完美,完结撒花