简体   繁体   English

如何从挂载的函数修改全局变量? Vue.js

[英]How to modify a global variable from a mounted function? Vue.js

I'm new to Vue.js and I couldn't find an answer online.我是 Vue.js 的新手,在网上找不到答案。 I have a function in 'mounted:' and I need it to update a global variable.我在 'mounted:' 中有一个函数,我需要它来更新全局变量。

Here is the code:这是代码:

<template>
    <div id="MovieContent">
        <h1>Content</h1>
        <div class="movie-details-container">
            <h3 class="movie-title">{{movieTitle}}</h3>
            <IMG class="movie-poster" :src="moviePosterURL" alt=""/>
            <p class="movie-description">{{movieDescription}}</p>
        </div>
    </div>
</template>

<script>
    import Vue from 'vue';
    import { MdField } from 'vue-material/dist/components'

    Vue.use(MdField);

    const API_URL = 'http://www.omdbapi.com/?apikey=API_KEY&';
    // eslint-disable-next-line no-unused-vars
    const POSTER_API_URL = 'http://img.omdbapi.com/?apikey=API_KEY&';

    const movieName = null;
    const moviePoster = null;
    const moviePlot = null;

    export default {
        data: () => ({
            movieTitle: movieName,
            moviePosterURL: moviePoster,
            movieDescription: moviePlot
        }),
        methods: {

        },
        mounted: function () {
            this.$root.$on('SEND_SEARCH_INPUT', (text) => {
                async function fetchMovie(text) {
                    try {
                        const movieDetails = await fetch(API_URL + `t=${text}`);
                        const myJSON = await movieDetails.json();

                        // eslint-disable-next-line no-console
                        console.log(myJSON['Title']);
                        // eslint-disable-next-line no-console
                        console.log(myJSON['Year']);
                        // eslint-disable-next-line no-console
                        console.log(myJSON['Poster']);
                        // eslint-disable-next-line no-console
                        console.log(myJSON['Plot']);
                    } catch (error) {
                        // eslint-disable-next-line no-console
                        console.log(error)
                    }
                }

                fetchMovie(text)

            })
        }
    }
</script>

<style>

</style>

I want 'fetchMovie(text)' to update -movieName -moviePoster -moviePlot.我想要 'fetchMovie(text)' 来更新 -movieName -moviePoster -moviePlot。

I have been reading the docs for a while now but nothing helps in this.我已经阅读了一段时间的文档,但没有任何帮助。 Maybe I'm not catching the idea of how to pass data from functions, components and else.也许我没有理解如何从函数、组件和其他地方传递数据。

Any advice?有什么建议吗? thanks a lot.多谢。

const movie = //your value

const keyword in JavaScript is used to define scope level constants. JavaScript 中的const关键字用于定义范围级别的常量。 const variable cannot be reassigned, it must be assigned while initialisation. const 变量不能重新赋值,必须在初始化时赋值。

const a;
a = 10;

This is absurd.这是荒谬的。 So instead of const use let所以代替const使用let

let movie =//your value

It will declare a scope level variable.它将声明一个范围级别的变量。 You can read more on the topics online.您可以在线阅读有关主题的更多信息。 As far as vue is concerned.就 vue 而言。

 <script>
  import Vue from 'vue';
  import { MdField } from 'vue-material/dist/components' Vue.use(MdField); 
 const API_URL = 'http://www.omdbapi.com/?apikey=API_KEY&'; // eslint-disable-next-line no-unused-vars
   const POSTER_API_URL = 'http://img.omdbapi.com/?apikey=API_KEY&'; 

 let movieName = null; 
 let moviePoster = null; 
 let moviePlot = null; 
 export default { 
 data: () => ({ movieTitle: movieName, moviePosterURL: moviePoster, movieDescription: moviePlot }), 
 methods: { },
 mounted: function () {
      movieName = this.movieName;
      moviePoster = this.moviePoster;
      MoviePlot = this.moviePlot;
  } 
} 
</script> 

Use this context inside the object to reference all the thing which is associated with vue.在对象内部使用此上下文来引用与 vue 关联的所有事物。

In Vue, you can directly modify whatever is declared in the data of your component.在 Vue 中,您可以直接修改组件数据中声明的任何内容。 It's a good practice to put your variables in one containing object.将变量放在一个包含对象中是一种很好的做法。 It's clean, and, mostly, Vue can't track changes if you overwrite the root of an object tree it's watching, so...它很干净,而且大多数情况下,如果覆盖了它正在监视的对象树的根,Vue 将无法跟踪更改,所以......

let vueStore = {
    movieName:null,
    moviePoster:null,
    moviePlot:null,
}
export default { 
data(){ return {vueStore: vueStore} }, 
methods: { },
mounted: function () {
    this.vueStore.movieName = this.movieName;
    this.vueStore.moviePoster = this.moviePoster;
    this.vueStore.MoviePlot = this.moviePlot;
} 
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM