時間:2023-07-08 11:12:01 | 來源:網(wǎng)站運營
時間:2023-07-08 11:12:01 來源:網(wǎng)站運營
如何用 Vue.js 實現(xiàn)一個建站應(yīng)用:隨著互聯(lián)網(wǎng)飛速發(fā)展,越來越多的傳統(tǒng)服務(wù)搬到了線上,商家急需一個官網(wǎng)介紹自己的產(chǎn)品,提高知名度。因此 “建站” 成為了一種剛需。本文就如何用 Vue.js 實現(xiàn)一個建站應(yīng)用提供了解決思路。作為前端工程師,相信大家都寫過不少網(wǎng)站和應(yīng)用,我把網(wǎng)站簡單的分為 “表現(xiàn)型” 和 “操作型”。表現(xiàn)型可以是一個產(chǎn)品介紹網(wǎng)站,而操作型的典型代表是管理后臺。不久前有機會參與一個建站項目的設(shè)計與開發(fā),它屬于操作類型網(wǎng)站但需要更深一層的抽象,它是一個 “創(chuàng)建網(wǎng)站的網(wǎng)站”。本文嘗試基于 Vue.js 框架設(shè)計實現(xiàn)這樣一個應(yīng)用,用戶通過拖拽模塊就可以建站。希望通過本文的介紹,能夠帶給大家不一樣的視角。
{ "id": 1, "name": "xxx公司" "type": "site", "children": [{ "type": "page", "name": "首頁", "children": [{ "type": "section", "name": "公司簡介", "children": [{ "type": "paragraph" }, { "type": "carousel" }] }] }]}
site.config
中增加themeColor
屬性來表示。class="col-xs-12 col-sm-6 col-md-3"
表示該板塊在手機下橫向占 100%、平板占 50%、PC 占 25%。node
屬性和themeColor
<!-- Paragraph.vue --><template> <div> <h1 :style="{color: themeColor}">{{node.content.title}}</h1> <small v-if="node.config.showSubTitle">{{node.content.subTitle}}</small> <p>{{node.content.detail}}</p> </div></template><script>export default { name: 'paragraph', props: ['node', 'themeColor']}</script>
完成所有節(jié)點代碼編寫之后,第二步,我們需要寫一個類似于 “renderer” 的組件來遞歸的渲染 JSON 樹?;舅悸肥窃摻M件先渲染自己,然后渲染自己的后代,每個后代也重復(fù)此渲染過程,如此渲染整棵樹。type
屬性也就是一個 String 來獲取對應(yīng)的組件定義。幸運的是 Vue.js 中已經(jīng)有這樣的動態(tài)組件Component
,此組件的is
屬性接受一個 String。由此我們的 render 組件可以這樣寫:<!-- render.vue --><tempplate> <component :is="node.type" :node="node" :theme="themeColor"> <render v-for="child in node.children" :key="child.id" :node="child" :theme="themeColor" /> </component></tempplate><script>// 導(dǎo)入JSON 樹中所涉及的所有節(jié)點import Page from './Page.vue'import Section from './Section.vue'import Paragraph from './Paragraph.vue'export default { name: 'render', props: ['node', 'themeColor'], components: { Page, Section, Paragraph }}</script>
注:若 Vue.js 沒有提供動態(tài) Component 組件,我們也可以利用 Vue.js 中的createElement
方法自己實現(xiàn)該組件,詳見此 gist(https://gist.github.com/github-libra/7ba6eb53efd5cccdd3828030708c4297)。site.config.themeColor
拖拽模塊到區(qū)域中在對應(yīng)的section.children
的數(shù)組中 push 一個組件節(jié)點在區(qū)域中排序模塊在對應(yīng)的section.children
的數(shù)組中重新設(shè)置組件節(jié)點的 index編輯模塊內(nèi)容和配置更新對應(yīng)模塊的 content 和 config 中的屬性保存網(wǎng)站把 JSON 樹存入數(shù)據(jù)庫持久化// store.jsimport Vue from 'vue'import Vuex from 'vuex'Vue.use(Vuex)const store = new Vuex.Store({ state: { site: {} }, mutations: { changeThemeColor () {}, addModule () {}, sortModule () {}, removeModule () {}, updateModule () {} }, actions: { getSite () {}, saveSite () {} }})
理論上有了這些方法我們已經(jīng)可以從通過程序更新這棵樹了,但作為編輯后臺,我們還要提供一些界面上的入口讓用戶來編輯這棵樹。edit-
。例如 Paragraph 這個組件的編輯組件可以編寫如下:<!-- EditParagraph.vue --><template> <edit-wrapper> <paragraph :node="node" :themeColor="themeColor" /> </edit-wrapper></template><script>// EditWrapper提供統(tǒng)一的編輯入口,內(nèi)部仍需要渲染一次 Paragraph 便于實時預(yù)覽編輯結(jié)果import EditWrapper from './EditWrapper.vue'import Paragraph from './Paragraph.vue'import { mapMutations } from 'vuex'export default { name: 'edit-paragraph', props: ['node', 'themeColor'], methods: { ...mapMutations(['updateModule']) }}</script>
用戶可以通過這個組件的界面入口編輯 Paragraph 這個模塊(此處略去edit-wrapper
的實現(xiàn),事實上組件的編輯可以通過彈窗加表單實現(xiàn),也可以是更方便的 inline editing )。Vue.Draggable
(github.com/SortableJS/Vue.Draggable)這個庫,它是基于 Sortable.js 做的一層封裝。其典型的應(yīng)用場景如下:<draggable v-model="myArray" :options="{group:'people'}" @start="drag=true" @end="drag=false"> <div v-for="element in myArray">{{element.name}}</div></draggable>
在我們的場景中,只需在draggable
組件上監(jiān)聽add
和sort
事件調(diào)用 store 中對應(yīng)的方法即可。// store.jsconst autoSave = (store) => { store.watch( state => state.site, (newV, oldV) => { store.dispatch('saveSite') }, { deep: true } )}const store = new Vuex.Store({ state: { site: {} }, plugins: [autoSave]})
至此,我們已經(jīng)用 Vue.js 和相關(guān)技術(shù)實現(xiàn)了文章開頭列出的建站應(yīng)用的需求。作者:唐鶴俊
簡介:百姓網(wǎng)前端工程師。
本文僅為作者個人觀點,不代表百姓網(wǎng)立場。
關(guān)鍵詞:實現(xiàn)
微信公眾號
版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。