Skip to content

Commit

Permalink
fix(router): 修复navigateBack的传参错误,添加错误提示
Browse files Browse the repository at this point in the history
  • Loading branch information
Littly committed Dec 10, 2018
1 parent 1e7dc10 commit 55f9aed
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions packages/taro-router/src/apis/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { History } from '../utils/types'
import invariant from 'invariant';

type SuccessCallback = (res: any) => any
type FailCallback = (err: any) => any
Expand Down Expand Up @@ -27,6 +28,9 @@ interface RedirectToOption {

const createNavigateTo = (history: History) => {
return function ({ url }: NavigateToOption) {

invariant(url, 'navigateTo must be called with a url')

try {
if (/^(https?:)\/\//.test(url)) {
window.location.assign(url);
Expand All @@ -40,10 +44,13 @@ const createNavigateTo = (history: History) => {
}

const createNavigateBack = (history: History) => {
return function (opts: NavigateBackOption = { delta: -1 }) {
return function (opts: NavigateBackOption = {}) {
try {
const { delta = -1 } = opts
history.go(delta)
const { delta = 1 } = opts

invariant(delta >= 0, 'navigateBack must be called with a delta greater than 0')

history.go(-delta)
return Promise.resolve()
} catch (e) {
return Promise.reject()
Expand All @@ -53,6 +60,9 @@ const createNavigateBack = (history: History) => {

const createRedirectTo = (history: History) => {
return function ({ url }: RedirectToOption) {

invariant(url, 'redirectTo must be called with a url')

if (/^(https?:)\/\//.test(url)) {
window.location.assign(url);
}
Expand Down

0 comments on commit 55f9aed

Please sign in to comment.