好的,我有你的问题。在我看来,您正在尝试将模板与项目集成:)
在/resources/js/ 中创建新文件externalJS.js 并粘贴以下代码
function head_script(src) {
if(document.querySelector("script[src='" + src + "']")){ return; }
let script = document.createElement('script');
script.setAttribute('src', src);
script.setAttribute('type', 'text/javascript');
document.head.appendChild(script)
}
function body_script(src) {
if(document.querySelector("script[src='" + src + "']")){ return; }
let script = document.createElement('script');
script.setAttribute('src', src);
script.setAttribute('type', 'text/javascript');
document.body.appendChild(script)
}
function del_script(src) {
let el = document.querySelector("script[src='" + src + "']");
if(el){ el.remove(); }
}
function head_link(href) {
if(document.querySelector("link[href='" + href + "']")){ return; }
let link = document.createElement('link');
link.setAttribute('href', href);
link.setAttribute('rel', "stylesheet");
link.setAttribute('type', "text/css");
document.head.appendChild(link)
}
function body_link(href) {
if(document.querySelector("link[href='" + href + "']")){ return; }
let link = document.createElement('link');
link.setAttribute('href', href);
link.setAttribute('rel', "stylesheet");
link.setAttribute('type', "text/css");
document.body.appendChild(link)
}
function del_link(href) {
let el = document.querySelector("link[href='" + href + "']");
if(el){ el.remove(); }
}
export {
head_script,
body_script,
del_script,
head_link,
body_link,
del_link,
}
然后在您的App.vue 或app.js 中粘贴此
import * as external from '../externalJS.js';
export default
{
mounted()
{
external.body_script('/path/to/your.js');
external.head_link('/path/to/style.css');
}
destroyed()
{
external.del_link('/path/to/your.js');
external.del_link('/path/to/style.css');
}
}
externlJS.js解释
- 如果要从特定组件中删除脚本,则调用函数
del_script()
- 如果要在
<head>标签中添加脚本(JS文件),则调用函数head_script()
- 如果要在
<body>标签中添加脚本,则调用函数body_script()
- 如果要添加css文件,则调用函数
body_link()
- 如果要在
<body>标签中添加css样式,则调用函数body_script()
- 如果要从特定组件中删除css stlye,请调用函数
del_link()
希望对你有所帮助:)