【问题标题】:vue svg path with v-forvue svg 路径与 v-for
【发布时间】:2019-03-26 16:10:39
【问题描述】:

我正在尝试将 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)
            },

我在不调用方法的情况下尝试执行此操作的方式不正确吗?

谢谢

【问题讨论】:

    标签: svg vue.js path v-for


    【解决方案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>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-12
      • 2018-06-13
      • 1970-01-01
      相关资源
      最近更新 更多