【发布时间】:2020-03-21 04:44:38
【问题描述】:
我是 Vue.js 的新手,在网上找不到答案。我在 'mounted:' 中有一个函数,我需要它来更新全局变量。
代码如下:
<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>
我希望 'fetchMovie(text)' 更新 -movieName -moviePoster -moviePlot。
我已经阅读了一段时间的文档,但对此没有任何帮助。也许我没有理解如何从函数、组件等传递数据。
有什么建议吗?非常感谢。
【问题讨论】:
-
你试过movieName = 什么吗?等
-
你怎么知道它不起作用?您的陈述处于尝试中。是否有任何内容记录到控制台?
-
它没有将新值分配给变量。控制台按预期记录 API 请求。
标签: javascript vue.js vuejs2 vue-component