Vuejs在路由后加载js文件
问题描述:
我正在使用vuejs路由器构建单页应用程序。 路由器工作正常,但我想在路由到组件后加载特定的脚本。Vuejs在路由后加载js文件
水木清华这样的:
const Mine = {
template: ' <div>Another</div>',
load_script: <script src="/static/script.js"></script>
}
我怎样才能做到这一点?
现在我有当前块的html:
<div id="routing">
<router-view></router-view>
</div>
这是JS:
const Mine = {template: ' <div>Another</div>'}
const routes = [
{ path: '/mine', component: Mine },
]
const router = new VueRouter({
routes,
})
const app = new Vue({
router
}).$mount('#routing')
答
您可以加载脚本的HTML模板本身,因为它是做加载任何文件从HTML,您可以有以下在您的模板:
const Mine = {
template: '<div> \
<script src="/static/script.js"></script> \
<div>Another</div> \
</div>',
}
Smth奇怪发生在我身上。即使'const bar = {template:'
@Snobby尝试在div中的'template'块中包装所有内容,如下所示:'const Bar = {template:'
'div's inside ''div运作良好,谢谢。但是这个技巧不适用于'script'。所以'const bar = {template:'