v-v的vue svg路径

时间:2018-10-22 14:31:24

标签: svg vue.js path v-for

我正在尝试将svg路径中的各种坐标绑定到vue.js中的data()

<svg xmlns="http://www.w3.org/2000/svg" width="500" height="500">
       <g>
           <path v-for="n in 50" d="'M0 ' + (n*10) + ' L500 ' + (n*10)" fill="white" stroke="lightgray" />
           <path v-for="n in 50" d="{'M0 ' + (n*10) + ' L500 ' + (n*10)}" fill="white" stroke="lightgray" />
       </g>
</svg>

替代方法是调用一个有效的方法,

<path v-for="n in 400" :d="gridy(n)" fill="white" stroke="lightgray" />

gridy()方法

gridy(n) {
                return "M0" + " " + (n * 10) + " L500 " + (n * 10)
            },

这是我尝试不调用方法错误的方式吗?

谢谢

1 个答案:

答案 0 :(得分:1)

您只需要bind的SVG的d属性,如下所示:

<path v-for="n in 50" :d="'M0 ' + (n*10) + ' L500 ' + (n*10)" fill="white" stroke="lightgray" />

工作示例:

new Vue({el: '#app'})
Vue.config.devtools = false
Vue.config.productionTip = false
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script>
<main id="app">
<svg xmlns="http://www.w3.org/2000/svg" width="500" height="500">
       <g>
           <path v-for="n in 50" :d="'M0 ' + (n*10) + ' L500 ' + (n*10)" fill="white" stroke="lightgray" />
       </g>
</svg>
</main>