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

Using 'this' in channel:'user.' + this.user.id #13

Closed
GordonChen13 opened this issue Oct 5, 2017 · 4 comments
Closed

Using 'this' in channel:'user.' + this.user.id #13

GordonChen13 opened this issue Oct 5, 2017 · 4 comments

Comments

@GordonChen13
Copy link

can I do something like this to apply current vue instance scope in channel: 'user.' + this.user.id

channel: function (vm) {
return 'user.' + vm.user.id;
}
@happyDemon
Copy link
Owner

If the user property has been defined before you return it here it should be fine.

@GordonChen13
Copy link
Author

GordonChen13 commented Oct 6, 2017

But I got error like this:

[Vue warn]: Error in created hook: "TypeError: e.startsWith is not a function"

app.js:6812 TypeError: e.startsWith is not a function
    at app.js:99918
    at VueComponent.created (app.js:99918)
    at callHook (app.js:8911)
    at VueComponent.Vue._init (app.js:10472)
    at new VueComponent (app.js:10642)
    at createComponentInstanceForVnode (app.js:9924)
    at init (app.js:9741)
    at app.js:9941
    at createComponent (app.js:11393)
    at createElm (app.js:11336)

even console.log(vm) get the same error. I think channel: function (vm) {
return 'user.' + vm.user.id;
} wasn't support yet. I can use this.$echo.channel() function to get what I need , But if it can support
channel:..... ,it is better. Thanks a lot!

@happyDemon
Copy link
Owner

oh, I'm sorry, I read through that too quickly while I was at work.

but you're right, only strings are currently supported. I'll see if I can whip something up this weekend.

My main worry is though, is that the channel gets initialised on the created hook.
So the user info, or at least the id should be present before that happens.
Accessing the VM does not seem like the most logical option, unless you're pulling in data and setting it to the VM in the beforeCreate hook.

@tomlankhorst
Copy link

tomlankhorst commented Sep 11, 2018

You could join a channel in mounted and unsubscribe manually in beforeDestroy.

export default { 
  // data: ... 
  mounted: function(){
    this.$echo.channel('My.Channel.'+this.me); //... etc. 
  },
  beforeDestroy: function(){
    this.$echo.leave('My.Channel.'+this.me)
  }
}

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

Successfully merging a pull request may close this issue.

3 participants