To ask you a quick question, the array rendering page

render the page according to the one-dimensional array

Apr.19,2021

define instructions

 // common.js 

export const join = {

  bind (el, binding, vnode) {
    let value = binding.value
    if (Object.prototype.toString.call([]) !== Object.prototype.toString.call(value)) {
      throw new Error('')
    }
    value.forEach((item, index) => {
        // -
      if (index + 1 === value.length) {
        el.innerHTML += `<a href="${item.url || 'javascript:void(0)'}">${item.name}</a>`
      } else {
        el.innerHTML += `<a href="${item.url || 'javascript:void(0)'}">${item.name} - </a>`
      }
    })
  }
}

introduce use

// xxx.vue

<template>
      <div v-join="joinData"></div>
</template>
<script>
import { join } from "@/directives/common";
export default {
  name: "app",
  directives: {
    join
  },
  data(){
    return{
     // 
     joinData: [
        { name: "a", url: "http://www.baidu.com" },
        { name: "b", url: "http://www.jd.com" },
        { name: "c", url: "" } // 
     ]
    }
  }
}
</script>

this way feels reusable. I don't know if you can use this data format.


this effect?

<div>
  <a v-for="item in arr" 
     :key="item"
     :href="item">
    {{ item }} -
  </a>
</div>

<a v-for:"item in arr" :key="item.index" :bind></a> 

like this?


not just one layer v-for

Menu