How do I get my ref-activated dialog to appear?

I am using Vue 3 and Vuetify 3. I am trying to understand the various ways to make a dialog appear, based on the examples in the Vuetify documentation, especially the four methods illustrated in the first example within the Slots section of the Dialogs article. At the moment, I’m struggling with the ref-activated method. This playground illustrates the issue.

To see the code, go down to the card titled “Approach 3: Ref-activated”. I’ve got a button that is trying to invoke a dialog in component RADialog1. The problem is that I can’t figure out how to pass the ref from that button to the dialog so that it opens when I click on the Ref Activator 1 button.

I’ve tried passing the ref as a prop but the defineProps macro objects to me passing something called ref and then typescript-annotating it as a ref. I tried using provide in the parent and inject in the dialog but I keep getting syntax errors, no matter what I try.

How can I pass the ref value from the parent to the dialog?

I can’t see anything in the Vuetify docs that explains this. (Unfortunately, their examples all show dialogs that are coded within the parent component being invoked. I think it’s a lot better to code dialogs as separate components.)

Refs can be passed down just like any prop. You don’t necessarily need provide/inject.

App.vue

<v-btn class="indent" color="indigo" ref="trigger1"
  >Ref Activator 1
</v-btn>
<RADialog1 :verbiage="ra_prop1" :activator="trigger1" />
const trigger1 = ref()

Side note: When defining a ref, the value wrapped by the ref is considered the initial value. You originally had ref('raDialog1'), which is a ref with initial string value “raDialog1”, but once the component mounts, this initial value is overwritten with the VBtn component. It’d be better to just let the initial value be null, e.g. ref()

RADialog1

<v-dialog max-width="360" persistent :activator="activator">
const props = defineProps(['verbiage', 'activator'])

You mentioned typescript error before. If you want typescript to be happy, you can define props like this

<script setup lang="ts">
  import type { PropType, ComponentPublicInstance } from 'vue'

  const props = defineProps({
    verbiage: { type: String },
    activator: {
      type: Object as PropType<ComponentPublicInstance>
    }
  })
</script>

Playground example

Simpler Alternative

There’s actually a shortcut here since the root element of RADialog is v-dialog, assigning the activator to RADialog will have it fallthrough to the v-dialog (provided you don’t declare the activator as a prop in RADialog).

App.js

<RADialog1 :verbiage="ra_prop1" :activator="trigger1" />

Now just don’t declare any activator prop in RADialog1.

RADialog1.vue

<v-dialog max-width="360" persistent>
const props = defineProps(['verbiage'])

Playground example

1

There are three issues in your code:

  • When providing a ref, use the ref itself, not its value:
  const trigger1 = ref('raDialog1')
  provide('refActivtor1', trigger1) // <--- instead of trigger1.value

Using trigger1.value would provide the data inside the ref at the moment provide() is called. This happens during setup, when the component is not yet mounted, so the ref will be undefined.

  • Typo: you provide 'refActivtor1' (missing ‘a’ in ‘vtor1’), but try to resolve it as 'refActivator1' in RADialog1:
const refActivator1 = inject('refActivator1')

Note there is a warning in the console about a missing inject.

  • The activator prop of VDialog is given as regular string attribute (activator="raDialog1" resolves to the string 'raDialog1'), but it should be a Vue data binding with either v-bind:activator or the shorthand :activator:
<v-dialog 
  :activator="refActivator1"

Now the content of the ‘refActivator1’ variable is bound to the prop.

Here is the updated playground

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật