代码之家  ›  专栏  ›  技术社区  ›  ofey

带v-for的vue svg路径

  •  0
  • ofey  · 技术社区  · 6 年前

    我试图将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 回复  |  直到 6 年前
        1
  •  1
  •   Sølve T. DisgruntledGoat    6 年前

    你只需要 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>
    推荐文章