如何定义vue模板中的变量?

flseospp  于 7个月前  发布在  Vue.js
关注(0)|答案(8)|浏览(81)

问题

我需要在Vue模板中临时存储方法调用的结果。这在循环内部特别常见,在那里我不能很容易地使用计算属性。

<ul>
  <li v-for="vehicleType in vehicleTypes" :key="vehicleType">
    <h3>{{ vehicleType }}</h3>
    <div v-if="getVehicleTypeData(vehicleType)">
     {{ getVehicleTypeData(vehicleType).costPerMile }}<br>
     {{ getVehicleTypeData(vehicleType).costPerHour }}<br>
    </div>
  </li>
</ul>

字符串

JavaScript程式码片段:

getVehicleTypeData: function(vehicleType){
    let options = _.find(this.vehicleTypeOptions, (obj)=>{
      return obj.vehicleType==vehicleType;
    });

    return options;
}


为了提高性能,我确实需要一个变量来存储方法调用结果。
Vue解决此问题的方法是什么?

vulvrdjw

vulvrdjw1#

解决Vue当前缺点的一个快速方法是通过v-for和单个循环使用作用域。一个希望解释的例子:

<v-list>
  <v-list-tile v-for="(module, idx) in modules">
    <template v-for="scope in [{ isLocked: someFunction(module)}]">
      <!-- any markup -->
      <v-list-tile-action v-if="scope.isLocked">
        <v-icon color="amber">lock</v-icon>
      </v-list-tile-action>
    </template>
  </v-list-tile>
</v-list>

字符串
上面的<template>元素可以做到这一点。您可以在一个临时的size-1对象数组中调用您的函数(someFunction),将其分配给一个属性(isLocked),然后将其分配给一个作用域变量(scope)。现在您可以访问someFunction返回的任何内容,只要您愿意,而不会通过scope.isLocked牺牲性能。

00jrzges

00jrzges2#

在模板中的任何地方,你都可以用**:set=“VAART = 12”**来修改代码,神奇的是,VAART变量会被设置(但不会是被动的)。
在VUE 3上测试了这个魔术

new9mtju

new9mtju3#

你可以创建一个computed属性,将类型obj合并到vehiclesTypes数组中。

computed: {

  vehicles() {
    return this.vehicleTypes.map(vehicle => {
       return {
         value: vehicle,
         type: { ...this.getVehicleTypeData(vehicle) }
       }
    })
  }

},

methods: {
  getVehicleTypeData(vehicleType){
    let options = _.find(this.vehicleTypeOptions, (obj)=>{
      return obj.vehicleType==vehicleType;
    });

    return options;
  }
}

字符串
你可以做:

<ul>
  <li v-for="vehicle of vehicles" :key="vehicle.value">
    <h3>{{ vehicle.value }}</h3>
    <div v-if="vehicle.type">
     {{ vehicle.type.costPerMile }}<br>
     {{ vehicle.type.costPerHour }}<br>
    </div>
  </li>
</ul>


如果你遵循逻辑,我相信它会工作。虽然我不知道vehiclesTypes的值,所以上面的代码可能需要一些修改。
希望能帮到你。

xcitsw88

xcitsw884#

一种选择是定义一个组件。您可以将需要“存储”的值作为 prop 传递给它,它可以以多种方式使用它。这是更像Vue的方法。
另一种选择是将函数调用 Package 在数组中,并使用v-for为其创建别名。这更像是一种黑客/懒惰的优化,但它并不脆弱,只是读起来很奇怪。

new Vue({
  el: '#app',
  data: {
    vehicleTypes: [0, 1]
  },
  methods: {
    getVehicleTypeData(type) {
      return [{
        costPerMile: 10,
        costPerHour: 40
      }][type];
    }
  }
});

个字符

lzfw57am

lzfw57am5#

我从一些研究中找到了一个解决方案,我自己回答,但不确定是否有其他最好的解决方案。

JavaScript程式码片段:

const Pass = {
  render() {
    return this.$scopedSlots.default(this.$attrs)
  }
}

export default {
  components: {
    Pass
  },
  data: function () {
    return {
      vehicleTypeOptions: [],
    }
  },
  methods: {
    getVehicleData: function(vehicleType){
      let option = _.find(this.vehicleTypeOptions, (obj)=>{
        return obj.vehicleType==vehicleType;
      });
      return option;
    },
    loadData: function(){
      // get data from server using API and set to vehicleTypeOptions
    }
  },
  mounted: function(){
    this.loadData();
  }
}

字符串

模板片段:

<Pass v-for="vehicleType in VehicleTypes" v-bind:key="vehicleType" :temp="getVehicleData(vehicleType)">
  <div slot-scope="{temp}">
    <div class="pannel">
        <h6>{{ vehicleType }}</h6>
        <p v-if="temp">
          Cost per mile: <strong>${{ temp.costPerMile }}</strong>, 
          Cost per hour: <strong>${{ temp.costPerHour }}</strong>, 
          Cost per day: <strong>${{ temp.costPerDay }}</strong>
        </p>
    </div>
  </div>
</Pass>

zmeyuzjn

zmeyuzjn6#

在目前的版本中,这是不可能的:(
在你的例子中,你可以用计算值来计算并使用它。

<ul>
  <li v-for="vehicleType, idx in vehicleTypes" :key="vehicleType">
    <h3>{{ vehicleType }}</h3>
    <div v-if="vtData[idx]">
     {{ vtData[idx].costPerMile }}<br>
     {{ vtData[idx].costPerHour }}<br>
    </div>
  </li>
</ul>

...

computed: {
  vtData() {
    return this.vehicleTypes.map(vt => this.getVehicleTypeData(vt));
  }
}

字符串

oyt4ldly

oyt4ldly7#

正如Fernando塞萨尔已经提到的,有一个未记录的特性:set,只是一个片段和一些链接

<ul>
  <li v-for="vehicleType in vehicleTypes" 
    :key="vehicleType"
    :set="vtd = getVehicleTypeData(vehicleType)"
  >
    <h3>{{ vehicleType }}</h3>
    <div v-if="vtd">
     {{ vtd.costPerMile }}<br>
     {{ vtd.costPerHour }}<br>
    </div>
  </li>
</ul>

字符串
https://github.com/vuejs/core/issues/1172#issuecomment-1035369827 www.example.com

f8rj6qna

f8rj6qna8#

这可能是一个可靠的策略:

<template>
  <div :style="{ opacity: 0, position: 'absolute' }">{{ variable = -Infinity }}</div>
  {{ variable }}
</template>

<script>
export default {
  mounted () {
    // The variable is actually added to the component.
    console.log (this.variable)
  }
}
</script>

字符串
可能还有一种方法可以在模板循环中定义变量。

相关问题