Coder Social home page Coder Social logo

vue-taro-router's Introduction

VueTaroRouter

基本 Taro ,扩展成 vue-router 的使用体验

Installation

npm i @noahsun/taro-vue-router -S
yarn add @noahsun/taro-vue-router

Usage

// router.js
import VueTaroRouter from "@noahsun/taro-vue-router";
const router = new VueTaroRouter();

// 添加拦截器
router
  .beforeEach((to, from, next) => {
    console.log(to);
    console.log(from);
    next();
  })
  .beforeEach((to, from, next) => {
    if (~to.path.indexOf("abc2")) {
      // 在拦截器里重定向
      next({
        path: "/pages/home/index",
        success() {
          console.log("success");
        }
      });
    } else {
      next();
    }
  })
  .afterEach((to, from) => {
    console.log("afterEach:", to);
    console.log("afterEach:", from);
  });

export default router;
// app.js
import Vue from "vue";
import router from "./router.js";

Vue.use(router);
// page.vue
this.$router.push({
  path: "/page/abc2/index",
  query: { id: "123" },
  events: {
    // 为指定事件添加一个监听器,获取被打开页面传送到当前页面的数据
    acceptDataFromOpenedPage: function (data) {
      console.log(data);
    },
    someEvent: function (data) {
      console.log(data);
    }
  },
  complete() {
    // 不论成功失败,都会执行这里
  },
  fail(err) {
    // 失败,执行这里
    console.log(err);
  },
  success: function (res) {
    // 成功,执行这里
    // 通过eventChannel向被打开页面传送数据
    res.eventChannel.emit("acceptDataFromOpenerPage", { data: "test" });
  }
});

Methods

this.$router.push(options);
this.$router.replace(options);
this.$router.back(options);
this.$router.relaunch(options);
this.$router.switchTab(options);

// 添加前置拦截器
this.$router.beforeEach(beforeEach);

// 添加后置响应器
this.$router.afterEach(afterEach);

options为字符串时

this.$router.push('/path?a=1&b=2');
this.$router.replace('/path?a=1&b=2');
this.$router.back('/path?a=1&b=2');
this.$router.relaunch('/path?a=1&b=2');
this.$router.switchTab('/path?a=1&b=2');

options为对象时路由跳转入参

属性 类型 必填 描述
path string 路径 (replace、back 方法可不传)
delta number 返回的页面数,如果 delta 大于现有页面数,则返回到首页。默认为 1
query object 查询参数
events object 页面间通信接口,用于监听被打开页面发送到当前页面的数据
complete function 接口调用结束的回调函数(调用成功、失败都会执行)
fail function 接口调用失败的回调函数
success function 接口调用成功的回调函数

属性

currentRoute, query、path、fullPath

this.$router.currentRoute.query;
this.$router.currentRoute.path;
this.$router.currentRoute.fullPath;

// 或
this.$route.query;
this.$route.path;
this.$route.fullPath;

vue-taro-router's People

Contributors

noahsun avatar kaeryehaowan avatar

Stargazers

 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.