Vue.js 选项卡切换效果代码无法运行?常见问题及解决方案
这段代码无法运行的原因可能有以下几点:
-
缺少引入 Vue.js 的代码,需要在
<head>标签中添加以下代码:<script src='https://cdn.jsdelivr.net/npm/vue/dist/vue.js'></script> -
在 Vue 实例中的
methods中的函数需要用 ES6 箭头函数来定义,修改为以下代码:changeTab: (tab) => { if (tab === 'hit') { this.currentView = 'hit'; this.isHitmovie = true; this.isClassicmovie = false; } else if (tab === 'classic') { this.currentView = 'classic'; this.isHitmovie = false; this.isClassicmovie = true; } } -
在组件的
template中,需要使用 ES6 模板字符串,即在反引号``中编写 HTML 代码,修改为以下代码:template:<div class='main'><span>{{ index + 1 }}</span>{{ movie.name }}<span>{{ movie.star }}</span><button @click='removeMovie(index)'>删除
修改以上代码后,应该可以正常运行该页面。