Vue determine if component has mounted - vue.js

Is there a built-in way of determining if the component has mounted in Vue?
I know I could just do mounted() { this.hasMounted = true; } but I would like to know if there already was a property for this.

Found that _isMounted is getting set by Vue.

you can use it to search for your component if it mounted
this.$options.components[findComponentName]
or using v-ref like this one <comp v-ref:comp-name></comp>
and you can use it from the $refs object if(this.$refs.compName)

Related

Why aren't there props for child -> parent and $emits for parent -> child?

I have a page with a component and the page needs to access a variable in that component. Would be nice if it were reactive. Then from the page I need to activate a function in the component. Would be nice if it could be done without a reactive variable. My question is 1: what's the best way to activate the function from the parent, for example when I click a button and 2: it seems very unintuitive and random to me that they aren't both possible in both directions? Anyone maybe know how Vue suggest you do it? This whole thing seems so complex relative to the relatively simple thing I'm trying to do.
I guess I try to use props? Or are refs a better option here?
So in general: you use refs, if you need the dom element, that's the whole purpose of refs. Since you don't mention that you n ed the dom element, you don't need to use that here.
There are 3 ways of communication: parent to child via props: https://vuejs.org/guide/components/props.html
child to parent via events
https://vuejs.org/guide/components/events.html
and anyone to anyone via event bus, which need an extra lib in vue3 and is out of scope for your question
https://v3-migration.vuejs.org/breaking-changes/events-api.html#event-bus
If you want to execute a function in the component whenever the value changes, you can put a watcher on the prop.
The other way around, from child to parent, you just create a listener to your emitted event and invoke a function of your choice. There are good examples in the docs in my opinion.
As per my understanding, You want to trigger the child component method from the Parent component without passing any prop as a input parameter and in same way you want to access child component data in the parent component without $emit. If Yes, You can simply achieve this using $refs.
You can attach the ref to the child component and then access it's variables and methods with the help of this $refs.
Live Demo (Just for a demo purpose I am using Vue 2, You can make the changes as per Vue 3) :
Vue.component('child', {
data: {
childVar: ''
},
methods: {
triggerEventInChildFromParent() {
console.log('Child Function triggered from Parent');
}
},
mounted() {
this.childVar = 'Child component variable'
}
});
var app = new Vue({
el: '#app',
methods: {
triggerEventInChild() {
this.$refs.child.triggerEventInChildFromParent()
}
},
mounted() {
console.log(this.$refs.child.childVar)
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<button #click="triggerEventInChild">
Button in Parent
</button>
<child ref="child"></child>
</div>

How does Vue use this.$route.query in a JS file

I want to use this.$route.query in the JS file ,but this point undefined
console.log(this, "query"); //undefined
console.log(this.$route.query); //error
The easiest way is to create a global variable for it.
In your component:
mounted() {
window.$route = this.$route
}
Then you can access it from anywhere as window.$route.query.
If you need to make it reactive, just create a watcher for $route and update your global variable in it.

Preferred way to access props in Vue Single File Component

Suppose I have a prop named message which I want to access from the script section of a .vue file.
I know that it can be accessed using this.$props.message and this.message from the data function.
Which is the preferred way to access props from different lifecycle hooks (created, mounted, etc), and from computed getters, and methods?
Component properties as well as passed in props should always be referenced to by this.propName, because you shouldn't assign a component property with the same name as a passed in prop. In this case Vue will respond with an error.
As Aer0 said, they shouldn't have the same names:
props: ['propMessage'],
data() {
return {
message: ''
};
},
created() {
console.log(this.propMessage);
console.log(this.message);
}

Vue | Get all component data attributes

I am trying to get all of the Vue components data properties from within a mixin that I am using for my plugin.
I have got the plugin registering correctly and the mixin is actively working.
I have added a lifecycle hook from within the Vue.mixin which runs on the created() event.
I am trying to loop through all of the components data and assign it to a property within my plugin, this is because I want to manipulate all of the data in every single component in my application.
I have tried to do the following:
Vue.mixin({
created() {
console.log(this.$options.data());
}
})
But i get the result saying that data is not a function. However, calling within the () on the end of the data logs a function out which I can see the data within.
Does anyone have any ideas?
Thank you
Try to use this.$data instead:
Vue.mixin({
created() {
console.log(this.$data);
}
});
>> DEMO on CodeSandbox

Vue.js - How to pass non-reactive data to child components

I know we can pass data to child components via Props. But it is reactive in one-way data flow mode. If the value of the data is changed in Parent component, it also has effect (update) on props in Child component.
In my case, I don't want to get update on specific prop in Child component even if that data in the Parent component is changed. It is because Child component will only responsible to show the data. But the data in the Parent Component still has to be reactive in Parent Scope.
I've read some forum article that suggest to use like Json. I feel it is a little dirty way and the data in my case is just one string.
Is there anyways to achieve that kind of solution?
You could copy the reactive prop in the created hook of the child component. The copy would not be reactive e.g.
export default {
props: {
reactive: Object
},
data: () => ({
nonreactive: null
}),
created() {
this.nonreactive = Object.assign({}, this.reactive)
}
}
Note: the way you copy the reactive prop will depend on the data type, the way I've shown will work for objects.
Maybe u can check this one
VueJS render once into an element
use v-once on your child component
Use prop as data property in child component. please see the fiddle link:
link here
Vue.component('greeting', {
props: ['user'],
data:function(){
return {
newuser:this.user
}
},
template: '<h1>hi {{ newuser }}</h1>'
});