Home  >  Article  >  Web Front-end  >  Detailed explanation of steps to implement WeChat public account menu editor with Vue.js (Part 1)

Detailed explanation of steps to implement WeChat public account menu editor with Vue.js (Part 1)

php中世界最好的语言
php中世界最好的语言Original
2018-05-23 10:56:192575browse

This time I will bring you Vue.js to implement the WeChat public account menuEditorDetailed steps (Part 1), Vue.js to implement the WeChat public account menu editorNotes What are they? Here are actual cases. Let’s 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 a method to the vue instance

In the vue instance give Add our custom method to the methods object

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 = &#39;&#39;
 }
 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 when the add button is clicked, the selectedMenu method is triggered. Add 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:

JS calculation of pi to 100 decimal places implementation steps detailed explanation

vue axios production environment and release Detailed explanation of the steps for configuring different interface addresses in the environment

The above is the detailed content of Detailed explanation of steps to implement WeChat public account menu editor with Vue.js (Part 1). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn