Skip to content

Commit

Permalink
fix: onError 无法调用 vue 实例上方法的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
aOrz committed Mar 20, 2018
1 parent 5c24878 commit c17f568
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
12 changes: 7 additions & 5 deletions src/platforms/mp/runtime/lifecycle.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { handleError } from '../../../core/util/index'

export function callHook (vm, hook, params) {
const handlers = vm.$options[hook]
let handlers = vm.$options[hook]
if (hook === 'onError') {
handlers = [handlers]
}

let ret
if (handlers) {
Expand Down Expand Up @@ -75,16 +78,15 @@ export function initMP (mpType, next) {
mp.app = this
mp.status = 'launch'
this.globalData.appOptions = mp.appOptions = options

callHook(rootVueVM, 'onLaunch')
callHook(rootVueVM, 'onLaunch', options)
next()
},

// Do something when app show.
onShow (options = {}) {
mp.status = 'show'
this.globalData.appOptions = mp.appOptions = options
callHook(rootVueVM, 'onShow')
callHook(rootVueVM, 'onShow', options)
},

// Do something when app hide.
Expand Down Expand Up @@ -160,7 +162,7 @@ export function initMP (mpType, next) {
mp.query = query
mp.status = 'load'
getGlobalData(app, rootVueVM)
callHook(rootVueVM, 'onLoad')
callHook(rootVueVM, 'onLoad', query)
},

// 生命周期函数--监听页面显示
Expand Down
18 changes: 13 additions & 5 deletions test/mp/runtime/lifecycle.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { createInstance } = require('../helpers/index')
// 生命周期
describe('init mpvue with lifecycle', function () {
const onLifecycle = []
const getOptions = {}

function getComponentOptions (key = '') {
return {
Expand Down Expand Up @@ -38,13 +39,16 @@ describe('init mpvue with lifecycle', function () {
onLifecycle.push(`destroyed${key}`)
},
// lifycycle for wxmp
onLaunch () {
onLaunch (opt) {
getOptions.onLaunch = opt
onLifecycle.push(`onLaunch${key}`)
},
onLoad () {
onLoad (opt) {
getOptions.onLoad = opt
onLifecycle.push(`onLoad${key}`)
},
onShow () {
onShow (opt) {
getOptions.onShow = opt
onLifecycle.push(`onShow${key}`)
},
onReady () {
Expand Down Expand Up @@ -83,11 +87,14 @@ describe('init mpvue with lifecycle', function () {
app.$mount()
expect(onLifecycle).toEqual(['beforeCreate', 'created', 'onLaunch', 'beforeMount', 'mounted', 'onShow'])
expect(!!app.$mp.app).toEqual(true)
expect(app.$mp.appOptions).toEqual({
const opt = {
path: 'pages/index/index',
scene: 1001,
query: {}
})
}
expect(app.$mp.appOptions).toEqual(opt)
expect(getOptions.onLaunch).toEqual(opt)
expect(getOptions.onShow).toEqual(opt)
expect(app.$mp.mpType).toEqual('app')
expect(app.$mp.status).toEqual('show')
})
Expand All @@ -108,6 +115,7 @@ describe('init mpvue with lifecycle', function () {
expect(onLifecycle).toEqual(['beforeCreate', 'created', 'onLoad', 'onShow', 'onReady', 'beforeMount', 'mounted'])
expect(!!app.$mp.page).toEqual(true)
expect(app.$mp.query).toEqual({})
expect(getOptions.onLoad).toEqual({})
expect(app.$mp.appOptions).toEqual({
path: 'pages/index/index',
scene: 1001,
Expand Down

0 comments on commit c17f568

Please sign in to comment.