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

Vue 3组件API计算函数

  •  0
  • user759235  · 技术社区  · 5 年前

    export default {
        props: {
           classProp: {type: String},
           error: {type: String},            
        },
        setup(){
         
            // move to here (this is not working)
            computed(() => {
              const classObject = () => {
                return ['form__control', this.classProp, 
                    {              
                        'form__invalid': this.error
                    }
                ]
              }
            })
        },
        computed: {
             classObject: function () {
                 return ['form__control', this.classProp, 
                     {              
                         'form__invalid': this.error
                     }
                 ]
             }
        },
    }
    
    0 回复  |  直到 5 年前
        1
  •  0
  •   Vasili    5 年前

    一起跳过“计算” 您需要使用“ref”或“reactive”。这些是模块:

    <script>
        import { ref } from 'vue'
        setup(){
         const whateverObject = ref({ prop: "whatever initial value" });
         whateverObject.value.prop= "if you change something within setup you need to access it trough .value";
         return { whateverObject } // expose it to the template by returning it
        }
    </script>
    

    如果要使用类,请像我自己的示例中那样导入它们:

    import { APIBroker } from '~/helpers/APIbroker'
    const api = new APIBroker({})
    

    现在,“api”可以在setup()内部或任何地方使用

    推荐文章