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

Modal无法在Vue/Firebase应用程序中工作,因为未定义“$”

  •  0
  • user21885339  · 技术社区  · 2 年前

    这就是它给我的错误:

    33:7未定义错误“$”

    这是AddToCart.js中的内容。模态格式在我的产品列表组件中。

    <template>
    <div class="add-to-cart">
    
      <button class="btn btn-primary" @click="addToCart" style="background-color: black; color: whitesmoke; width: 80px; font-size: small">
        Add To Cart
      </button>
    
    </div>
    </template>
    
    <script>
    export default {
      name: "Add-to-cart",
      props: {
        name: String,
        price: Number,
        ID: String,
      },
    
      data(){
        return {
          record: {
            recordName: this.name,
            recordPrice: this.price,
            recordID: this.ID,
          },
       }
    },
    
      methods: {
        addToCart() {
          $('#myModal').modal('show')
          this.$store.commit('addToCart', this.record);
        },
      }
    };
    
    
    </script>
    
    <style scoped>
    
    </style>
    

    我试着从引导程序中导入$,但这给了我一个新的错误,即$(“#myModal”).modal(“ow”)不是函数。我似乎就是无法让模态工作起来。

    0 回复  |  直到 2 年前
        1
  •  0
  •   Tolbxela    2 年前

    您正试图使用jQuery打开您的模态。

     $('#myModal').modal('show')
    

    检查是否 jQuery library 已加载。这是 jQuery CDNs

        2
  •  0
  •   Mauli999    2 年前

    您正在使用引导程序,因此可以使用

    this.$bvModal.show("modal-id");
    

    打开您的模态。(这可能取决于您的Bootstrap版本,对于Bootstrap vue可能是明确的)

    另一种方式是

    this.$refs["modal-ref"].show();
    
        3
  •  0
  •   theMountainMan    2 年前

    我不建议使用JQuery$符号来访问元素,因此, 正如官方文件aso所表明的那样

    注意:建议使用 this.$bvModal.show() this.$bvModal.hide() 方法(在上一节中提到),而不是使用$ref方法。

    您可以使用, 这个$bvModal.show() 显示和 这个$bvModal.hide() 隐藏模态

    此外,这种方法太适用了

    this.$refs['my-modal'].show()
    this.$refs['my-modal'].hide()