Go模板后端渲染時vue單頁面沖突
go后端模版語法是通過 {{}} ,vue也是通過雙花括號來渲染的,如果使用go渲染vue的html頁面的時候就會報錯,因為分別不出來哪個是vue的,哪個是go的,既可以修改go的模板語法
template.New("output").Delims("{%", "%}")
也可以修改vue的
new Vue({
delimiters: ['${', '}'],
el: '#vue-app',
})
但是由于我在golang的編輯器中,在html文件類型改為go模板時,不想看到語法報錯,所以就修改vue的。并且由于我的組件多,且復用的html多,所以我需要抽離公共的部分。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vue.js Delimiters Example</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
</head>
<body>
<div id="app">
<component-one></component-one>
<component-two></component-two>
</div>
<script>
// 定義 mixin
var myMixin = {
data: function () {
return {
message: 'Hello from mixin!'
}
}
}
// 定義組件 ComponentOne
Vue.component('component-one', {
mixins: [myMixin],
data: function () {
return {
message: 'Hello from mixin1111!'
}
},
template: '<div>${ message }</div>',
delimiters: ['${', '}'] // 設置分隔符
});
// 定義組件 ComponentTwo
Vue.component('component-two', {
mixins: [myMixin],
template: '<div>${ message }</div>', // 使用相同的分隔符
delimiters: ['${', '}'] // 設置分隔符
});
new Vue({
el: '#app'
});
</script>
</body>
</html>
這種已經(jīng)可以實現(xiàn),但是每個組件的template可能是一樣的,并且也不是上面那種簡單沒有class等信息的,所以需要抽離,所以就變成了下面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vue.js Delimiters Example</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
</head>
<body>
<div id="app">
<component-one></component-one>
<component-two></component-two>
</div>
<script>
// 定義 mixin
var myMixin = {
data: function () {
return {
message: 'Hello from mixin!'
}
}
}
// 復雜的共享模板字符串
var sharedTemplate = `
<div class="my-component">
<p>${message}</p>
<!-- Add your complex HTML structure and styles here -->
</div>
`;
// 定義組件 ComponentOne
Vue.component('component-one', {
mixins: [myMixin],
template: sharedTemplate,
delimiters: ['${', '}'] // 設置分隔符
});
// 定義組件 ComponentTwo
Vue.component('component-two', {
mixins: [myMixin],
template: sharedTemplate, // 使用相同的分隔符
delimiters: ['${', '}'] // 設置分隔符
});
new Vue({
el: '#app'
});
</script>
</body>
</html>
這種運行后你會發(fā)現(xiàn),無法渲染,控制臺報錯

怎么回事,語法也沒錯,分隔符設置也沒問題,但提示沒有定義,猜測是`符號影響了(不確定,有懂的call我),
想要解決這個問題
法一,模板中替換
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vue.js Delimiters Example</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
</head>
<body>
<div id="app">
<component-one></component-one>
<component-two></component-two>
</div>
<script>
// 定義 mixin
var myMixin = {
data: function () {
return {
message: 'Hello from mixin!'
}
}
}
// 復雜的共享模板字符串
var sharedTemplate = `
<div class="my-component">
<p>$MESSAGE$</p>
<!-- Add your complex HTML structure and styles here -->
</div>
`;
// 定義組件 ComponentOne
Vue.component('component-one', {
data: function () {
return {
message: 'Hello from mixin1111!'
}
},
mixins: [myMixin],
template: sharedTemplate.replace('$MESSAGE$', '${message}'),
delimiters: ['${', '}'] // 設置分隔符
});
// 定義組件 ComponentTwo
Vue.component('component-two', {
mixins: [myMixin],
template: sharedTemplate.replace('$MESSAGE$', '${message}'), // 使用相同的分隔符
delimiters: ['${', '}'] // 設置分隔符
});
new Vue({
el: '#app'
});
</script>
</body>
</html>
可以渲染,但是麻煩,傳遞幾個變量就得替換幾次

法二:和法一類似,在生成模板時處理
<body>
<div id="app">
<component-one></component-one>
<component-two></component-two>
</div>
<script>
// 定義 mixin
var myMixin = {
data: function () {
return {
message: 'Hello from mixin!'
}
}
}
// 生成帶有動態(tài)值的模板字符串
function generateTemplate(message) {
return `
<div class="my-component">
<p>${message}</p>
<!-- Add your complex HTML structure and styles here -->
</div>
`;
}
// 定義組件 ComponentOne
Vue.component('component-one', {
data: function () {
return {
message: 'Hello from mixin1111!'
}
},
mixins: [myMixin],
template: generateTemplate('${message}'),
delimiters: ['${', '}'] // 設置分隔符
});
// 定義組件 ComponentTwo
Vue.component('component-two', {
mixins: [myMixin],
template: generateTemplate('${message}'), // 使用相同的分隔符
delimiters: ['${', '}'] // 設置分隔符
});
new Vue({
el: '#app'
});
</script>
</body>
</html>
可以渲染,但是比較麻煩,單獨傳值

法三(推薦,簡單)
模板字面量,使用vue變量的地方帶上\轉(zhuǎn)義,無需修改其它
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vue.js Delimiters Example</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
</head>
<body>
<div id="app">
<component-one></component-one>
<component-two></component-two>
</div>
<script>
// 定義 mixin
var myMixin = {
data: function () {
return {
message: 'Hello from m1!',
msg: 'Hello from m2!'
}
}
}
// 使用模板字面量定義模板字符串
var sharedTemplate = `
<div class="my-component">
<p>\${message}</p>
<p>\${msg}</p>
<!-- Add your complex HTML structure and styles here -->
</div>
`;
// 定義組件 ComponentOne
Vue.component('component-one', {
data: function () {
return {
message: 'Hello from mixin1111!',
msg: 'Hello from mixin2222!'
}
},
mixins: [myMixin],
template: sharedTemplate,
delimiters: ['${', '}'] // 設置分隔符
});
// 定義組件 ComponentTwo
Vue.component('component-two', {
mixins: [myMixin],
template: sharedTemplate, // 使用相同的分隔符
delimiters: ['${', '}'] // 設置分隔符
});
new Vue({
el: '#app'
});
</script>
</body>
</html>
如下

然后在數(shù)據(jù)渲染時使用golang的模板語法替換數(shù)據(jù)進行渲染即可
到此這篇關(guān)于Go模板后端渲染時vue單頁面沖突的文章就介紹到這了,更多相關(guān)Go vue 單頁面沖突內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Go語言同步等待組sync.WaitGroup結(jié)構(gòu)體對象方法詳解
這篇文章主要為大家介紹了Go語言同步等待組sync.WaitGroup結(jié)構(gòu)體對象方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-08-08
詳解Go語言如何利用高階函數(shù)寫出優(yōu)雅的代碼
高階函數(shù)(Hiher-order?Function)定義為:滿足下列條件之一的函數(shù):接收一個或多個函數(shù)作為參數(shù);返回值是一個函數(shù)。本文為大家介紹了如何利用高階函數(shù)寫出優(yōu)雅的代碼,希望對大家有所幫助2023-01-01
詳解Golang中創(chuàng)建error的方式總結(jié)與應用場景
Golang中創(chuàng)建error的方式包括errors.New、fmt.Errorf、自定義實現(xiàn)了error接口的類型等,本文主要為大家介紹了這些方式的具體應用場景,需要的可以參考一下2023-07-07
Bililive-go 實現(xiàn)直播自動監(jiān)控錄制功能
最近有直播錄制的需求,但是自己手動錄制太麻煩繁瑣,于是用了開源項目Bililive-go進行全自動監(jiān)控錄制,對Bililive-go 直播自動監(jiān)控錄制實現(xiàn)思路感興趣的朋友,一起看看吧2024-03-03

