Comments (12)
Using event bus is not the point here (but, according to vuex documentation, using a event bus should be used only on small applications, due to the complexity it could bring).
About the events itself, I agree:
- Events are scoped to the component ( like @miljan-aleksic said);
- Events should be written in any way the developer desires ( like @kartsims said);
- Events named using kebab case pleases me and most dev's I know;
- Bus event is not the issue here, so I don't see even why mention it (like @kartsims and @Elfayer said).
So this article from Google pretty much summarizes what this style guide will be. Did I get everything right?
from vuejs-component-style-guide.
I don't believe saying "all caps events are specific to my app" are a good way to answer the question "what is the right way to name components' events ?"
As @miljan-aleksic said, you can name your events whatever you want since it is only available in the component that listen to the event.
I agree with the kebab-case policy.
from vuejs-component-style-guide.
About the global bus event pattern, it works the same, if the bus doesn't listen to a specific event, no collision should happen...
I haven't ever used one either as I don't believe it's good practice but I would be curious to see an example too.
from vuejs-component-style-guide.
Yes, just don't forget the kebab case choice is not only beacuse pleases the most but also because assures the dom templates compatibility.
from vuejs-component-style-guide.
Hello again @miljan-aleksic :D
About that... I usually use all uppercase with underscore as spaces.
Why? Because I'm 100% sure nobody else does that. I mean, I can clearly separate a third party component event from the one I wrote.
Here. You can clearly see the event I wrote.
But, I mean... My idea is too personal. So, before we write this style guide, what do you think about the way I create my components events?
from vuejs-component-style-guide.
Good morning @miljan-aleksic ! :D
I would like to have some people helping write this style ( @aristidesfl @kartsims @DannyFeliz @crowchirp )
I have some considerations:
- Native events are lower case;
- A bunch of third party components are shipped using camel or kebab case;
- Personally I think all components strict to my application should have a unique way to emit events (like I mentioned on previous comments, all upper case with underscore as spaces);
Google has a good web component guide and I think we could take some of their idea and put it right here.
from vuejs-component-style-guide.
I thought I answered this one... Sorry.
As components events are scoped to the component it is safe to name them without any further consideration but to work on both dom and string templates. I would keep it simple and leave any further styling to the App requirements.
from vuejs-component-style-guide.
I agree with @miljan-aleksic, events should be kebab case. Although it might be a good idea to find a nice way to avoid collisions with a global bus event pattern.
from vuejs-component-style-guide.
Seems I'm missing the picture, probably because never used a bus event before. @Elfayer, can you expose an example where that could happen?
from vuejs-component-style-guide.
This is what it looks like: https://jsfiddle.net/6aes0mmL/
HTML
<template id="comp">
<div id="comp">
<button @click="bus.$emit('my-event', 'bus')">Emit from bus</button>
<button @click="$emit('my-event', 'comp')">Emit from component</button>
</div>
</template>
<div id="app">
<comp @my-event="addEvent"></comp>
<div>{{ events }}</div>
</div>
JS
// Bus event
var bus = new Vue();
// comp Component
var comp = {
template: '#comp',
data() {
return {
bus,
};
},
};
// Root View
new Vue({
el: '#app',
data: {
events: [],
},
methods: {
addEvent(v) {
this.events.push(v);
},
},
components: {
comp,
},
created() {
bus.$on('my-event', this.addEvent);
},
});
I think it's a bad practice too, maybe it's best not to mention it.
I believe it was mentioned by Evan You for people willing to migrate from Vue1 to Vue2 and having a mess of events.
from vuejs-component-style-guide.
OK I see the point but still, it is the developer's responsibility to know how to handle events depending on where it has been listened on, isn't it ?
bus.$on('my-event', ...
could be handled differently from <comp @my-event="...
Like I said, I see the point, but when you listen to mouseenter
event on a DOM element you should pass on the element to the event handler for an appropriate response.
Edit : I may be missing something because I don't think this is an issue at all. So perhaps like you said it is best not to mention it...
from vuejs-component-style-guide.
Moved this issue to a PR soon.
from vuejs-component-style-guide.
Related Issues (20)
- "options" should be "props" HOT 1
- Remove use-component-name-as-style-scope HOT 5
- Docute docs translation links are broken HOT 5
- About watch and methods HOT 2
- broken url HOT 2
- 错别字
- [New] When should I create a new component ? HOT 12
- Directory Structure Patterns HOT 7
- Section "Assign this to component" example does no show it HOT 6
- How about a complementing application HOT 3
- Application structure ? HOT 3
- Component naming section is confusing to me HOT 2
- /deep/ selector HOT 7
- Add standalone templates as suggestion HOT 2
- Suggest 'components' be placed before 'props' HOT 2
- 中文翻译中的一个小错误 HOT 2
- Component's name in folder HOT 1
- 'Keep component props primitive' - typos in sample code HOT 3
- #use-script-inside-component is not found HOT 1
- 句子勘误
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vuejs-component-style-guide.