Soy muy nuevo en VUE, así que disculpe si mi terminología no es correcta.
Estoy tratando de establecer una variable que se define en la función "datos ()" de la etiqueta del script.
Estoy tratando de establecer un nuevo valor para una variable definida en data() desde dentro del evento de ciclo de vida "creado()". Esto funciona bien si se hace en el nivel raíz, pero necesito hacerlo dentro de 2 llamadas anidadas como se muestra a continuación y no funcionará cuando esté anidado dentro de las llamadas a funciones:
aplicación.vue
<template> <div class="container"> <Button @btn-click="providerPicked" id="current-provider" :text="'Current Provider: ' + currentProvider" /> </div> </template> <script> import { ZOHO } from "./assets/ZohoEmbededAppSDK.min.js"; import Button from './components/Button' export default { name: 'App', components: { Button, }, data() { return{ currentProvider: 'x' } }, created() { console.log("CREATED HOOK") ZOHO.embeddedApp.on("PageLoad",function(data) { console.log(data); //Custom Business logic goes here let entity = data.Entity; let recordID = data.EntityId[0]; ZOHO.CRM.API.getRecord({Entity:entity,RecordID:recordID}) .then(function(data){ console.log(data.data[0]) console.log(data.data[0].name) //THIS DOES NOT WORK - variable still comes back 'x' in template, notice this is nested twice. this.currentProvider = data.data[0].name; }); }); ZOHO.embeddedApp.init(); //THIS DOES WORK - SETS VAR TO "reee" in template, notice this is not nested this.currentProvider = "reee" } } </script>Utilice funciones de flecha en lugar de funciones anónimas.
Las funciones de flecha no vinculan this y, por lo tanto, this se referirá al ámbito externo.
created() { console.log("CREATED HOOK") ZOHO.embeddedApp.on("PageLoad", (data) => { console.log(data); //Custom Business logic goes here let entity = data.Entity; let recordID = data.EntityId[0]; ZOHO.CRM.API.getRecord({ Entity: entity, RecordID: recordID }) .then((data) => { console.log(data.data[0]) console.log(data.data[0].name) //THIS DOES NOT WORK - variable still comes back 'x' in template, notice this is nested twice. this.currentProvider = data.data[0].name; }); }); ZOHO.embeddedApp.init(); //THIS DOES WORK - SETS VAR TO "reee" in template, notice this is not nested this.currentProvider = "reee" }