Using a Vue composition function in a regular Vue component - vue.js

The documentation says this is possible but there's no example showing how. How would one go about it?

The setup() method is like an add-on to a regular component so we can just include it along with data, methods, etc.
function useSomething() {
// ...
}
const Component = {
setup() {
const { out1, out2 } = useSomething();
return { out1, out2 };
},
data: function () {
// ...
},
methods: {
// ...
},
}

Related

Returning Apollo useQuery result from inside a function in Vue 3 composition api

I'm having some issues finding a clean way of returning results from inside a method to my template using Apollo v4 and Vue 3 composition API.
Here's my component:
export default {
components: {
AssetCreationForm,
MainLayout,
HeaderLinks,
LoadingButton,
DialogModal
},
setup() {
const showNewAssetModal = ref(false);
const onSubmitAsset = (asset) => {
// how do I access result outside the handler function
const { result } = useQuery(gql`
query getAssets {
assets {
id
name
symbol
slug
logo
}
}
`)
};
}
return {
showNewAssetModal,
onSubmitAsset,
}
},
}
The onSubmitAsset is called when user clicks on a button on the page.
How do I return useQuery result from the setup function to be able to access it in the template? (I don't want to copy the value)
You can move the useQuery() outside of the submit method, as shown in the docs. And if you'd like to defer the query fetching until the submit method is called, you can disable the auto-start by passing enabled:false as an option (3rd argument of useQuery):
export default {
setup() {
const fetchEnabled = ref(false)
const { result } = useQuery(gql`...`, null, { enabled: fetchEnabled })
const onSubmitAsset = (asset) => {
fetchEnabled.value = true
}
return { result, onSubmitAsset }
}
}
demo

VueJS How to access Mounted() variables in Methods

I'm new in Vue and would like assistance on how to access and use variables created in Mounted() in my methods.
I have this code
Template
<select class="controls" #change="getCatval()">
Script
mounted() {
var allcards = this.$refs.allcards;
var mixer = mixitup(allcards);
},
methods: {
getCatval() {
var category = event.target.value;
// I want to access mixer here;
}
}
I can't find a solution anywhere besides this example where I could call a method x from mounted() and pass mixer to it then use it inside my getCatval()
Is there an easier way to access those variables?
I will first suggest you to stop using var, and use the latest, let and const to declare variable
You have to first declare a variable in data():
data(){
return {
allcards: "",
mixer: ""
}
}
and then in your mounted():
mounted() {
this.allcards = this.$refs.allcards;
this.mixer = mixitup(this.allcards);
},
methods: {
getCatval() {
let category = event.target.value;
this.mixer
}
}
like Ninth Autumn said : object returned by the data function and props of your components are defined as attributes of the component, like your methods defined in the method attribute of a component, it's in this so you can use it everywhere in your component !
Here an example:
data() {
return {
yourVar: 'hello',
};
},
mounted() { this.sayHello(); },
method: {
sayHello() { console.log(this.yourVar); },
},
Update
you cannot pass any value outside if it's in block scope - Either you need to get it from a common place or set any common value
As I can see, var mixer = mixitup(allcards); is in the end acting as a function which does some operation with allcards passed to it and then returns a value.
1 - Place it to different helper file if mixitup is totally independent and not using any vue props used by your component
In your helper.js
const mixitup = cards => {
// Do some operation with cards
let modifiedCards = 'Hey I get returned by your function'
return modifiedCards
}
export default {
mixitup
}
And then in your vue file just import it and use it is as a method.
In yourVue.vue
import Helpers from '...path../helpers'
const mixitup = Helpers.mixitup
export default {
name: 'YourVue',
data: ...,
computed: ...,
mounted() {
const mixer = mixitup(allcards)
},
methods: {
mixitup, // this will make it as `vue` method and accessible through
this
getCatval() {
var category = event.target.value;
this.mixitup(allcards)
}
}
}
2- Use it as mixins if your mixitup dependent to your vue and have access to vue properties
In your yourVueMixins.js:
export default {
methods: {
mixitup(cards) {
// Do some operation with cards
let modifiedCards = 'Hey I get returned by your function'
return modifiedCards
}
}
}
And import it in your vue file:
import YourVueMixins from '...mixins../YourVueMixins'
const mixitup = Helpers.mixitup
export default {
name: 'YourVue',
mixins: [YourVueMixins] // this will have that function as vue property
data: ...,
computed: ...,
mounted() {
const mixer = this.mixitup(allcards)
},
methods: {
getCatval() {
var category = event.target.value;
this.mixitup(allcards)
}
}
}

How to add methods dynamically to Vue component

When I get data from the server, like
[{methodName:mothodValue},{methodName:mothodValue}]
then I want dynamic generation method, like
methods: {
functionArray.forEach(function (item) {
item.functionName:function () {
item.functionValue;
}
})
}
so this is my code
var componentName = Vue.component(componentName, {
data: function () {
return {
value: value
}
},
template: componentTemplate,
methods:{
functionArray.forEach(function (item) {
item.functionName:function () {
item.functionValue;
}
})
}
})
the old code was
methods:{
getValue : function(){
getValue(this.value);
}
}
Is that possible?
If the data from the server is returned before the creation of the Vue component (I'm not sure if you can add methods after the creation of a Vue component), then you can create a methods object like so:
var methods = {};
functionArray.forEach(function (item) {
methods[item.functionName] = item.functionValue;
});
You can't populate an object literal with code, but you can populate an empty object literal afterwards with code like so.
Then you can put it into your component like so:
var componentName = Vue.component(componentName, {
// ..
methods: methods
});

How to access nested functions in Vue.js?

Say, we have a Vue.js component built this way:
export default {
watch: {
// vars
}
},
methods: {
functionOne: function() {
function nestedFuncOne(p, q) {
// doing something
}
},
functionTwo: function() {
function nestedFuncTwo(r, s) {
// doing something
}
nestedFuncOne(param1, param2); // how to call it?
}
},
(...)
How should an inner function from the first method be called from inside of second method? this seems not to give desired output here.

How to pass a variable from a function in the component in Vue?

How to pass a variable from a function in the component in Vue?
This is my code:
export default {
name: 'app',
data: function () {
return{
city1: '',
city2: '',
metr: 0
}
},
created () {
ymaps.ready(init);
function init() {
$data.city1 = 'sdf'; // ?this to data of component?
Because you created an new function, the this inside it will be not point to the Vue component, but to the this of the function itself.
You can use an arrow function, or save the reference of the this, then use it later.
created() {
const self = this;
ymaps.init(init);
function init() {
self.city1 = 'sdf';
}
}
Or (better):
created() {
const init = () => {
this.city1 = 'sdf';
}
ymaps.init(init);
}