Go模板后端渲染時(shí)vue單頁(yè)面沖突
go后端模版語(yǔ)法是通過(guò) {{}} ,vue也是通過(guò)雙花括號(hào)來(lái)渲染的,如果使用go渲染vue的html頁(yè)面的時(shí)候就會(huì)報(bào)錯(cuò),因?yàn)榉謩e不出來(lái)哪個(gè)是vue的,哪個(gè)是go的,既可以修改go的模板語(yǔ)法
template.New("output").Delims("{%", "%}")
也可以修改vue的
new Vue({
delimiters: ['${', '}'],
el: '#vue-app',
})
但是由于我在golang的編輯器中,在html文件類型改為go模板時(shí),不想看到語(yǔ)法報(bào)錯(cuò),所以就修改vue的。并且由于我的組件多,且復(fù)用的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: ['${', '}'] // 設(shè)置分隔符
});
// 定義組件 ComponentTwo
Vue.component('component-two', {
mixins: [myMixin],
template: '<div>${ message }</div>', // 使用相同的分隔符
delimiters: ['${', '}'] // 設(shè)置分隔符
});
new Vue({
el: '#app'
});
</script>
</body>
</html>
這種已經(jīng)可以實(shí)現(xiàn),但是每個(gè)組件的template可能是一樣的,并且也不是上面那種簡(jiǎn)單沒(méi)有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!'
}
}
}
// 復(fù)雜的共享模板字符串
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: ['${', '}'] // 設(shè)置分隔符
});
// 定義組件 ComponentTwo
Vue.component('component-two', {
mixins: [myMixin],
template: sharedTemplate, // 使用相同的分隔符
delimiters: ['${', '}'] // 設(shè)置分隔符
});
new Vue({
el: '#app'
});
</script>
</body>
</html>
這種運(yùn)行后你會(huì)發(fā)現(xiàn),無(wú)法渲染,控制臺(tái)報(bào)錯(cuò)

怎么回事,語(yǔ)法也沒(méi)錯(cuò),分隔符設(shè)置也沒(méi)問(wèn)題,但提示沒(méi)有定義,猜測(cè)是`符號(hào)影響了(不確定,有懂的call我),
想要解決這個(gè)問(wè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 mixin!'
}
}
}
// 復(fù)雜的共享模板字符串
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: ['${', '}'] // 設(shè)置分隔符
});
// 定義組件 ComponentTwo
Vue.component('component-two', {
mixins: [myMixin],
template: sharedTemplate.replace('$MESSAGE$', '${message}'), // 使用相同的分隔符
delimiters: ['${', '}'] // 設(shè)置分隔符
});
new Vue({
el: '#app'
});
</script>
</body>
</html>
可以渲染,但是麻煩,傳遞幾個(gè)變量就得替換幾次

法二:和法一類似,在生成模板時(shí)處理
<body>
<div id="app">
<component-one></component-one>
<component-two></component-two>
</div>
<script>
// 定義 mixin
var myMixin = {
data: function () {
return {
message: 'Hello from mixin!'
}
}
}
// 生成帶有動(dòng)態(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: ['${', '}'] // 設(shè)置分隔符
});
// 定義組件 ComponentTwo
Vue.component('component-two', {
mixins: [myMixin],
template: generateTemplate('${message}'), // 使用相同的分隔符
delimiters: ['${', '}'] // 設(shè)置分隔符
});
new Vue({
el: '#app'
});
</script>
</body>
</html>
可以渲染,但是比較麻煩,單獨(dú)傳值

法三(推薦,簡(jiǎn)單)
模板字面量,使用vue變量的地方帶上\轉(zhuǎn)義,無(wú)需修改其它
<!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: ['${', '}'] // 設(shè)置分隔符
});
// 定義組件 ComponentTwo
Vue.component('component-two', {
mixins: [myMixin],
template: sharedTemplate, // 使用相同的分隔符
delimiters: ['${', '}'] // 設(shè)置分隔符
});
new Vue({
el: '#app'
});
</script>
</body>
</html>
如下

然后在數(shù)據(jù)渲染時(shí)使用golang的模板語(yǔ)法替換數(shù)據(jù)進(jìn)行渲染即可
到此這篇關(guān)于Go模板后端渲染時(shí)vue單頁(yè)面沖突的文章就介紹到這了,更多相關(guān)Go vue 單頁(yè)面沖突內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Go語(yǔ)言實(shí)現(xiàn)開(kāi)發(fā)一個(gè)簡(jiǎn)單的gRPC Demo
這篇文章主要為大家詳細(xì)介紹了如何利用Go語(yǔ)言實(shí)現(xiàn)開(kāi)發(fā)一個(gè)簡(jiǎn)單的gRPC Demo,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下2023-07-07
Go語(yǔ)言中如何實(shí)現(xiàn)并發(fā)
Go的并發(fā)機(jī)制通過(guò)協(xié)程和通道的簡(jiǎn)單性和高效性,使得編寫(xiě)并發(fā)代碼變得相對(duì)容易,這種并發(fā)模型被廣泛用于構(gòu)建高性能的網(wǎng)絡(luò)服務(wù)、并行處理任務(wù)和其他需要有效利用多核處理器的應(yīng)用程序,這篇文章主要介紹了在Go中如何實(shí)現(xiàn)并發(fā),需要的朋友可以參考下2023-09-09
Go語(yǔ)言實(shí)現(xiàn)優(yōu)雅關(guān)機(jī)和重啟的示例詳解
在Go語(yǔ)言中,實(shí)現(xiàn)優(yōu)雅關(guān)機(jī)和重啟通常涉及到處理系統(tǒng)信號(hào),并確保在關(guān)閉前完成所有必要的清理工作,下面我們就來(lái)看看如何使用http.Server和os/signal包來(lái)實(shí)現(xiàn)優(yōu)雅關(guān)機(jī)和重啟吧2025-05-05
Go語(yǔ)言底層原理互斥鎖的實(shí)現(xiàn)原理
這篇文章主要介紹了Go語(yǔ)言底層原理互斥鎖的實(shí)現(xiàn)原理,Go?sync包提供了兩種鎖類型,分別是互斥鎖sync.Mutex和讀寫(xiě)互斥鎖sync.RWMutex,都屬于悲觀鎖,更多相關(guān)內(nèi)容需要的朋友可以查看下面文章內(nèi)容2022-08-08
go-cqhttp權(quán)限管理系統(tǒng)的實(shí)現(xiàn)代碼
這篇文章主要介紹了go-cqhttp權(quán)限管理,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09

