vue移动端引用第三方组件-lytab(学习笔记)

栏目: JavaScript · 发布时间: 6年前

内容简介:在需要使用的组件中局部引用后来我发现还是报==========================================运用======================================

网络上有个大神自己写了一个 移动端可滑动(惯性滑动&回弹)Vue导航栏组件 ly-tab

然后我想把它用到我的项目中去,发现会有这个报错: SyntaxError: Unexpected token import

我也不知道是哪个步骤操作不正确。我就把它的源码从 node_modules 文件中复制出来,放在 components 文件下

node_modules

vue移动端引用第三方组件-lytab(学习笔记)

components

vue移动端引用第三方组件-lytab(学习笔记)

在需要使用的组件中局部引用

import LyTab from '@/components/Scroll'

后来我发现还是报 SyntaxError: Unexpected token import
接着尝试把路径换成下面的就不会报错了

import LyTab from '@/components/Scroll/src/index.vue'

==========================================运用======================================

<ly-tab :items="sortList" :options="options" class="parent" v-model="selected"></ly-tab>
items是传递给子组件的数组
options是lytab的配置
export default {
  components: {
    LyTab
  },
  data() {
    return {
      sortList: [], //父分类
      i: 0,
      selected: 0,
      options: {
        activeColor: '#78d5f7',
        labelKey: 'name' // 在sortList数组中选择想要渲染的key名
      }
    }
  }
}

后面我需要去获取点击父分类的index值从而筛选子分类,所以我需要在源码的基础添加

index.vue 子组件

<template>
  <div class="ly-tab">
    <ly-tabbar v-bind="options" v-model="selectedId">
      <ly-tab-item :key="index" v-for="(item, index) in items">
        <span slot="icon" v-if="options.fixBottom && item.icon">
          <i :class="item.icon"></i>
        </span>
        <span @click="getindex(index)">{{ item[labelKey] }}</span>
      </ly-tab-item>
    </ly-tabbar>
  </div>
</template>

新增getindex函数获取点击的index然后传递给父组件

父组件接收

<ly-tab :items="sortList" :options="options" @parent="parent" class="parent" v-model="selected"></ly-tab>
<div class="child" v-if="sortList[i].subCategoryList.length>0">
  <ul>
    <nuxt-link
      :key="index"
      :to="{path: '/quesbank/sort', query: {cp_id: item.pid, c_id: item.id}}"
      v-for="(item,index) of sortList[i].subCategoryList"
    >
      <li
        :class="$route.query.c_id===item.id?'activechild':''"
        v-if="item.name!==''"
      >{{item.name}}</li>
    </nuxt-link>
  </ul>
</div>
export default {
  components: {
    LyTab
  },
  data() {
    return {
      sortList: [], //父分类
      i: 0,
      selected: 0,
      options: {
        activeColor: '#78d5f7',
        labelKey: 'name'
      }
    }
  },
  mounted() {
    if (this.$route.query.cp_id) {
      this.selected = Number(this.$route.query.cp_id)
    }
  },
  //asyncData在服务端渲染
  async asyncData() {
    let res = await Net.getSort()
    let result = res.data.data
    result.unshift({
      name: '全部',
      id: 0,
      subCategoryList: []
    })
    for (let i = 0; i < result.length; i++) {
      if (result[i].subCategoryList) {
        for (let j = 0; j < result[i].subCategoryList.length; j++) {
          result[0].subCategoryList.push(result[i].subCategoryList[j])
        }
      }
    }
    result[0].subCategoryList.unshift({
      name: '全部',
      id: 0,
      pid: 0
    })
    return {
      sortList: result
    }
  },
  methods: {
  //获取index进行子分类的筛选
    parent(index) {
      this.i = index
    }
  }
}
</script>

写的很乱,是自己平时项目中的小总结


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Linux Programming Interface

The Linux Programming Interface

Michael Kerrisk / No Starch Press / 2010-11-6 / GBP 79.99

The Linux Programming Interface describes the Linux API (application programming interface)-the system calls, library functions, and other low-level interfaces that are used, directly or indirectly, b......一起来看看 《The Linux Programming Interface》 这本书的介绍吧!

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具