'''
[
"A", [
"A",
"B",
"C", [
"A",
"B",
"C"
]
],
"B",
"C"
]
'''
把上面的变成下面这样的,
'''
原生 js 办法是判断是否数组,是数组就创建列表,是字符串就创建子项然后迭代 但是没有找到 vue 怎么写
1
MacDows OP 感谢各位大佬!!!!
|
2
rabbbit 2022-02-08 20:44:01 +08:00 1
<template>
<ul v-if="Array.isArray(value)"> <deep v-for="(item, i) of value" :key="i" :value="item"> </deep> </ul> <li v-else> {{ value }} </li> </template> <script> export default { name: "Deep", props: { value: [Array, String], }, }; </script> |
3
rabbbit 2022-02-08 20:45:17 +08:00 1
<template>
<deep :value="value"></deep> </template> <script> import Deep from "@/components/deep/deep"; export default { components: { Deep }, data() { return { value: ["A", ["A", "B", "C", ["A", "B", "C"]], "B", "C"], }; }, }; </script> |
4
MacDows OP |
5
MacDows OP @rabbbit <!DOCTYPE html>
<html> <head> <meta charset='utf-8'> <meta http-equiv='X-UA-Compatible' content='IE=edge'> <!-- <title>Page Title</title> --> <meta name='viewport' content='width=device-width, initial-scale=1'> </head> <body> <script src='/source/js/vue.js'></script> <template> <ul v-if="Array.isArray(value)"> <deep v-for="(item, i) of value" :key="i" :value="item"> </deep> </ul> <li v-else> {{ value }} </li> </template> <script> export default { name: "Deep", props: { value: [Array, String], }, }; </script> </body> </html> |
7
rabbbit 2022-02-08 21:24:03 +08:00 1
new Vue({
el: "#app", components: { Deep: { name: "Deep", props: { value: [Array, String], }, template: ` <ul v-if="Array.isArray(value)"> <deep v-for="(item, i) of value" :key="i" :value="item"> </deep> </ul> <li v-else>{{ value }}</li> `, }, }, data() { return { value: ["A", ["A", "B", "C", ["A", "B", "C"]], "B", "C"], }; }, template: `<deep :value="value"></deep>`, }); |
8
rabbbit 2022-02-08 21:25:36 +08:00 1
new Vue({
el: "#app", components: { Deep: { name: "Deep", props: { value: [Array, String], }, template: ` <ul v-if="Array.isArray(value)"> <deep v-for="(item, i) of value" :key="i" :value="item"> </deep> </ul> <li v-else>{{ value }}</li> `, }, }, data() { return { value: ["A", ["A", "B", "C", ["A", "B", "C"]], "B", "C"], }; }, template: `<deep :value="value"></deep>`, }); |