Home >Web Front-end >JS Tutorial >How to use Vue.js to implement the WeChat public account menu editor (detailed idea)
This time I will show you how to use Vue.js to implement the WeChat public account menu editor (detailed idea), what are the precautions for using Vue.js to implement the WeChat public account menu editor, the following is a practical case, let's go together take a look.
I have been studying Vue.js for a while, so I want to try to make a menu editor like the one on the WeChat platform. I will share it here
Check the project github for the specific style code
Create a vue instance
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script src="https://cdn.bootcss.com/vue/2.5.9/vue.js"></script> </head> <body> <p class="content" style="width:900px;margin:0 auto;"> <!-- vue实例挂载的DOM元素 --> <p id="app-menu"> <!-- 菜单预览界面 --> <p class="weixin-preview"></p> <!-- 菜单编辑界面 --> <p class="weixin-menu-detail"></p> </p> </p> <script> var app = new Vue({ el: '#app-menu',//挂载到对应的DOM元素 data: { weixinTitle: 'Vue.js公众号菜单', //菜单对象 menu: { "button": [ { "name": "主菜单1", "sub_button": [] }, { "name": "主菜单2", "sub_button": [] }, { "name": "主菜单3", "sub_button": [ { "name": "子菜单1" }] }] }, selectedMenuIndex:'',//当前选中菜单索引 selectedSubMenuIndex:'',//当前选中子菜单索引 }, methods: { } }) </script> </body> </html>
Render the menu data to On the template
Here v-if and v-for are used to render data to the template. There will be up to 3 main menus and each main menu can have up to 5 submenus.
<p class="weixin-preview"> <p class="weixin-hd"> <p class="weixin-title">{{weixinTitle}}</p> </p> <p class="weixin-bd"> <ul class="weixin-menu"> <!-- 这里使用v-for开始循环主菜单 --> <li v-for="(btn,i) in menu.button" class="menu-item"> <p class="menu-item-title"> <span>{{ btn.name }}</span> </p> <ul class="weixin-sub-menu"> <!-- 这里使用v-for开始循环主菜单下的子菜单 --> <li v-for="(sub,i2) in btn.sub_button" class="menu-sub-item"> <p class="menu-item-title"> <span>{{sub.name}}</span> </p> </li> <!-- 这里使用v-if 判断子菜单小于5个,则添加按钮来添加子菜单 --> <li v-if="btn.sub_button.length<5" class="menu-sub-item"> <p class="menu-item-title"> <i class="icon14_menu_add"></i> </p> </li> </ul> </li> <!-- 这里使用v-if 判断主菜单小于3个,则添加按钮来添加主菜单 --> <li class="menu-item" v-if="menu.button.length<3"> <i class="icon14_menu_add"></i></li> </ul> </p> </p>
Add methods to the vue instance
Add our custom methods to the methods object in the vue instance
methods: { //选中主菜单 selectedMenu:function (i) { this.selectedSubMenuIndex = '' this.selectedMenuIndex = i }, //选中子菜单 selectedSubMenu:function (i) { this.selectedSubMenuIndex = i }, //选中菜单级别 selectedMenuLevel: function () { if (this.selectedMenuIndex !== '' && this.selectedSubMenuIndex === '') { //主菜单 return 1; } else if (this.selectedMenuIndex !== '' && this.selectedSubMenuIndex !== '') { //子菜单 return 2; } else { //未选中任何菜单 return 0; } }, //添加菜单 //参数level为菜单级别,1为主菜单、2为子菜单 addMenu:function (level) { if (level == 1 && this.menu.button.length < 3) { this.menu.button.push({"name": "菜单名称", "sub_button": [] }) this.selectedMenuIndex = this.menu.button.length - 1 this.selectedSubMenuIndex = '' } if (level == 2 && this.menu.button[this.selectedMenuIndex].sub_button.length < 5) { this.menu.button[this.selectedMenuIndex].sub_button.push({ "name": "子菜单名称" }) this.selectedSubMenuIndex = this.menu.button[this.selectedMenuIndex].sub_button.length - 1 } } }
Bind the method to the menu
When the menu is clicked, the selectedMenu method is triggered, and the add button is clicked to trigger the addMenu method. Use v-on to listen to the event , its abbreviation is @
to listen to the click event @click. In order to prevent the submenu click event from bubbling up the main menu, use the .stop event modifier To prevent bubbling @click.stop
Use v-bind:class to add the class when the switching menu is selected. :class is the abbreviation
<ul class="weixin-menu" id="weixin-menu" > <!-- 判断如果selectedMenuIndex是当前点击的主菜单索引则添加current样式 --> <li v-for="(btn,i) in menu.button" class="menu-item" :class="{current:selectedMenuIndex===i&&selectedMenuLevel()==1}" @click="selectedMenu(i)"> <p class="menu-item-title"> <span>{{ btn.name }}</span> </p> <!-- v-show来切换是否显示 这里如果选中了主菜单则子菜单弹出 --> <ul class="weixin-sub-menu" v-show="selectedMenuIndex===i"> <li v-for="(sub,i2) in btn.sub_button" class="menu-sub-item" :class="{current:selectedSubMenuIndex===i2&&selectedMenuLevel()==2}" @click.stop="selectedSubMenu(i2)"> <p class="menu-item-title"> <span>{{sub.name}}</span> </p> </li> <li v-if="btn.sub_button.length<5" class="menu-sub-item" @click.stop="addMenu(2)"> <p class="menu-item-title"> <i class="icon14_menu_add"></i> </p> </li> </ul> </li> <li class="menu-item" v-if="menu.button.length<3" @click="addMenu(1)"> <i class="icon14_menu_add"></i> </li> </ul>
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!
Recommended reading:
Detailed explanation of the steps to use the JS callback function
JS common algorithms accumulation, iteration, exhaustion, Recursive implementation (with code)
The above is the detailed content of How to use Vue.js to implement the WeChat public account menu editor (detailed idea). For more information, please follow other related articles on the PHP Chinese website!