Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

this.$on,this.$emit,this.$off 跨页面没效果 #32

Open
zhhcho opened this issue Jan 9, 2019 · 11 comments
Open

this.$on,this.$emit,this.$off 跨页面没效果 #32

zhhcho opened this issue Jan 9, 2019 · 11 comments

Comments

@zhhcho
Copy link

zhhcho commented Jan 9, 2019

No description provided.

@zhhcho
Copy link
Author

zhhcho commented Jan 9, 2019

是在组件中使用 this.$emit 然后在其他页面使用 this.$on进行监听,但在其他页面中this.$on 没有触发。

@switer
Copy link
Member

switer commented Jan 9, 2019

能确认 this.$on 是在 this.$emit之前执行吗

@zhhcho
Copy link
Author

zhhcho commented Jan 9, 2019

能确认this.$on是在this.$emit之前执行吗
我确实是先执行的派发。。 = = 谢谢你的提示,我对这块还不是很了解 不清楚监听一定要在 派发前执行。。。

@zhhcho
Copy link
Author

zhhcho commented Jan 9, 2019

能确认this.$on是在this.$emit之前执行吗

现在已经可以了 谢谢大大

@zhhcho zhhcho closed this as completed Jan 9, 2019
@zhhcho zhhcho reopened this Jan 9, 2019
@zhhcho
Copy link
Author

zhhcho commented Jan 9, 2019

能确认 this.$on 是在 this.$emit之前执行吗

require('./lib/wxpage').A({
extendPageBefore: function (name, page) {
page.$test = function(){
return 'ok';
}
},

通过这样挂载后 在页面中使用this.$test() 为什么会出现is not a function

挂载一个变量的话 在页面中打印也是只会出现undefined

@zhhcho
Copy link
Author

zhhcho commented Jan 9, 2019

感觉是和.A有关 但去掉的话 又会报其他错误。。

sdk uncaught third Error
Cannot read property 'comps' of undefined
TypeError: Cannot read property 'comps' of undefined

@switer
Copy link
Member

switer commented Jan 12, 2019

通过这样挂载后 在页面中使用this.$test() 为什么会出现is not a function

能展示下在哪个生命周期使用,与及完整的报错信息吗

@zhhcho
Copy link
Author

zhhcho commented Jan 15, 2019

通过这样挂载后 在页面中使用this.$test() 为什么会出现is not a function

能展示下在哪个生命周期使用,与及完整的报错信息吗

app.js

require('./lib/wxpage').A({
	extendPageBefore: function (name, page) {
		page.$global = function (key, value) {
			var len = arguments.length
			if (len > 1) {
			getApp().globalData[key] = value
			} else if (len == 1) {
			return getApp().globalData[key]
			} else {
			return getApp().globalData
			}
		}
	},
	globalData:{
		test:'111'
	},
	config: {
		route: '/pages/$page',
	},
	onLaunch: function(opts) {
		// console.log('APP is Running', opts)
	},
	onShow: function () {
		// Do something
	},
})

index.js

var P = require('../lib/wxpage');
P('index', {
	...
	onLoad:function(){

		console.log(this.$global('test'));
                
                ...

console

VM644:1 thirdScriptError
this.$global is not a function;at pages/index page lifeCycleMethod onLoad function
TypeError: this.$global is not a function
    at s.onLoad (http://127.0.0.1:13134/appservice/pages/index.js:22:20)
    at s. (http://127.0.0.1:13134/appservice/lib/wxpage.js:275:17)
    at s. (http://127.0.0.1:13134/appservice/lib/wxpage.js:275:17)
    at s. (http://127.0.0.1:13134/appservice/__dev__/WAService.js:1:696239)
    at s.__callPageLifeTime__ (http://127.0.0.1:13134/appservice/__dev__/WAService.js:1:696012)
    at Vt (http://127.0.0.1:13134/appservice/__dev__/WAService.js:1:714681)
    at Ht (http://127.0.0.1:13134/appservice/__dev__/WAService.js:1:715708)
    at Kt (http://127.0.0.1:13134/appservice/__dev__/WAService.js:1:717001)
    at Function. (http://127.0.0.1:13134/appservice/__dev__/WAService.js:1:718111)
    at pt. (http://127.0.0.1:13134/appservice/__dev__/WAService.js:1:683214)

@switer
Copy link
Member

switer commented Jan 18, 2019

extendPageBefore是在config下的,因为不属于app的生命周期:

A({
  config: {
    extendPageBefore: function (def, { fns }) {
      // ...
    }
  }
});

@zhhcho
Copy link
Author

zhhcho commented Jan 21, 2019

extendPageBefore是在config下的,因为不属于app的生命周期:

A({
  config: {
    extendPageBefore: function (def, { fns }) {
      // ...
    }
  }
});

extendPageBefore,extendPageAfter这两个的用法能不能在文档中有详细的介绍。。。 这个实在有点看不懂 怎么用。。。

@switer
Copy link
Member

switer commented Jan 21, 2019

extendPageBefore 是页面注册前执行,def是页面配置对象,可以往 def 挂载页面类方法(示例无关),你的用法是对的,只不过升级后 extendPageBefore 本来与 config同一层级,挪到了config里面

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants