2017-02-19 155 views
0

我正在使用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') 

回答

0

您可以加载脚本的HTML模板本身,因为它是做加载任何文件从HTML,您可以有以下在您的模板:

const Mine = { 
    template: '<div> \ 
        <script src="/static/script.js"></script> \ 
        <div>Another</div> \ 
       </div>',  
} 
+0

Smth奇怪发生在我身上。即使'const bar = {template:'

First
Second
'}'不起作用。只显示“第一”,不存在“第二”。因此只有你的例子执行脚本。不要看到'Another' – Snobby

+1

@Snobby尝试在div中的'template'块中包装所有内容,如下所示:'const Bar = {template:'

First
Second
'}' – Saurabh

+0

'div's inside ''div运作良好,谢谢。但是这个技巧不适用于'script'。所以'const bar = {template:'

First
'}'当然不是,也不是'const Bar = {template:'
First
' }'只执行第一个标签。 – Snobby