I've been digging into vue.js for the last 3 days, working with a vue cli 3 testproject.
I've some experience in object oriented programming, but not much.
Still, the "generic" approach of vue seemed familiar to me, echoing practices I've already seen in OOP.
Still, there are a lot of things boggling my mind and one of them are props.
I've sifted through several tutorials, both video and written form. Here is one pretty short I've read recently which illustrates the situation as I see it pretty short and nicely: https://reactgo.com/vuejs-props-tutorial/
So, to my understanding, props are ESTABLISHED in what I would deem a PARENT if we were in OOP. In the tutorial above, the author created button.vue where the props are declared and then exported to post.vue.
This, in my opinion, makes button.vue the parent of post.vue because post.vue inherits from button.vue. However, in generic programming or at least in vue, its vice versa Oo
ALL tutorials now (would) call post.vue the parent, because it specifies the data for the props for example through the html.
Post.vue
<template>
<div>
<h1>My first post</h1>
<p>This a big post helps us to learn vue props</p>
<my-button name="Share"></my-button>
<my-button name="Like"></my-button>
<my-button name="Comment"></my-button>
</div>
</template>
<script>
import Button from './Button.vue'
export default{
components:{
'my-button':Button
}
}
</script>
This really blows my mind when it comes to more complex components, where a view (using the terms a project using routing) consists of a component which was manufactured through a chain of multiple components building on each other.
Something like this:
MyView->listBuilder->listData1
I'm really struggling to understand how the technical side of vue works here.
I might as well take it as a given, but I'm pretty sure it won't be long before I run into problems because components building on components building on components (and so on) try to resolve properties which simply are not there yet, because the sequence in which the components inherit from each other was all juggled up.
It probably isn't a very good idea to build long chains of components building on each other, but the need might arise to do so at some point and then I'd like to have a good understanding of the ways vue handles this.
Furthermore, I'd really like to know the upsides of this approach in vue/generic programming.
It reminds me of interfaces in OOP, because I can specify in childcomponents what the parentcomponents MUST work with.
Like artoju already mentioned, don`t strictly apply OOP mindset.
In your example i dont see any data definition or prop definition.
Your View Component "post.vue" is the Root/Parent, inside there you are importing your Button component therefor it`s a child of "post.vue". Nothing to overthink.
Maybe this link can help you a bit more: https://forum.vuejs.org/t/how-can-i-make-oop-user-interfaces/10512/7
Related
I was reading answers to a Stack Overflow question when I came across this one, which has the file names in its example code ending in .view.
My experience with Vue has been that the single-file components always have their filenames end in .vue.
I looked online and couldn't find any other references to the .view extension.
Is there such a thing as a .view file in Vue? Or was this more likely a typo?
Considering the fact that the user who posted the answer has apparently no knowledge in Vue (or frontend), I assume this is quite a typo indeed.
There is no such thing in Vue. Especially since the answer is using script setup, which is quite specific to Vue, since it's some sugar syntax.
I've edited his answer to fix the typo.
I have notified the answerer of it, he may come back here at some point if he founds out that my edit was wrong.
Vue comes from View, and Vue is the french word for it. The confusion is understandable but there is no such things as .view.
There are no other tags like Laravel, Ruby on Rails or alike, so 100% a typo.
How does one access the filters defined on a Vue component (globally or locally) from within the render function? Neither the documentation nor a dump of this offer much help. The first trivial answer is to put the filter functions in an export, but that defeats the purpose of the Vue filter functionality. The second trivial answer is to import Vue, but that defeats the purpose of asking this in the first place.
The code in question is in a Nuxt context. I defined the filters in a file ~/plugin/filters.js, and updated the plugins configuration point in nuxt.config.js to run ~/plugin/filters.
this.$options does not have a filters property (although its prototype does).
I'm assuming functional must be false, but an answer that works for functional components is acceptable.
I'm working on a rather complex and big project. Our QA team decided they want unique id's to do automated testing. Since our developers are rather lazy (we are developers, you know?), we are trying to inject these id's automatically.
I've tried to create a mixin that tries to add an attribute on mounted.
mounted() {
this.$el.setAttribute('data-test-id', 'a-random-id-01');
}
But this fails all the time because $el is not always available right away. Does anyone have any insight how we could do this?
Find your question from google :)
this.$el.dataset.testId = 'a-random-id-01'
I am trying to better understand Vuejs lifecycles. The documentation is short and I guess assumes most people are familiar with the lifecycle concept.
However, I've only been using created(){} as it suits my need to initialize a function/data (to put it vaguely) when the "page is loaded".
But I am sure every lifecycle hook has its own distinct feature or purpose. So, I am hoping someone can provide a better intro/example of when each hook can be used from the context of page load to page finish.
To put it simply. When you request a web page, there are two observable steps
1: page is loading
2: page finished loading.
My question is, when do the created, mounted, updated and destroyed hooks take place within those two stages? I didn't mention the before/after hooks as they are obvious by their meaning.
Lifecycle hooks relate to a Vue instance, not to a page load. Components are Vue instances, as is the main Vue instance that is typically called on the whole page.
The diagram gives a pretty good rundown of where each hook sits in relation to what is going on on the instance. In simple terms, created happens before you have DOM to work with, and all the others happen after.
When I look at my application state in Vue's devtools chrome extension, I can see multiple instances of the same component in the tree view. However, there is no way to distinguish one instance from another. For example, in the screenshot below there are three <Todo> components. Imagine instead there are 30. How do I attach some kind of instance name or id to my components? It seems silly to have to click through 30 components to find the one I need.
I had the same idea also a couple of months ago.
So I gave it a try.
My PR has been submitted: https://github.com/vuejs/vue-devtools/pull/331
It's probably a bit early to rely on my solution since the core team could still reject the PR or request other naming conventions.
Until then you can build my proposal version yourself from my repo:
https://github.com/nerdyglasses/vue-devtools/tree/verbose-component-tree
Is that helping you?
Best, Christian
You can use the ':key' attribute. And you even HAVE TO use it if your component depends on a v-for loop.
here you can see the keys