Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usage in Vue framework template: passing dynamic data to the property #111

Closed
au-z opened this issue May 5, 2020 · 4 comments
Closed

Usage in Vue framework template: passing dynamic data to the property #111

au-z opened this issue May 5, 2020 · 4 comments
Labels
help properties Applies to the property definition

Comments

@au-z
Copy link
Contributor

au-z commented May 5, 2020

@smalluban - please feel free to close if you feel this is not the right venue for this question - It has more to do with the interaction between Vue and Hybrids than it does the Hybrids API.

I am having some trouble passing props from vue components to hybrids web components inside a .vue file template.

A brief, very simple example:
https://codesandbox.io/s/vue-hybrids-dynamic-prop-issue-dzis6?file=/src/App.vue

Though I've come to understand that Vue, by default passes props as properties (as opposed to attributes), it appears that this is not the behavior with a hybrids component in a Vue component template (you can see that the fallback attribute value appears to be updating), but the num property inside the component remains 0.

I was wondering if you had any feedback or suggestions on how to work around this problem? Thanks very much!

@smalluban
Copy link
Contributor

smalluban commented May 5, 2020

One of the design patterns in the hybrids library is using properties over attributes for dynamic values. It really makes sense, as attributes were designed for static HTML files (which basically don't change dynamically). Also, all of the built-in attributes are sync with the corresponding property on the DOM element.

The property factory only callbacks to the value of the attribute when the component is connected to the DOM. You can read more in one of the sections in the docs of the property factory.

As a result, you should notify the Vue template engine, that your intention is to set property, not an attribute. To do that, you can simply add .prop at the end of binding:

<template>
  <div id="app">
    <dynamic-prop-test :num.prop="num"></dynamic-prop-test>
    <br>
    Vue Num: {{num}}
  </div>
</template>

EDIT: Quote from the Vue docs:

Modifiers:

.prop - Bind as a DOM property instead of an attribute (what’s the difference?). If the tag is a > component then .prop will set the property on the component’s $el.

@smalluban smalluban added help properties Applies to the property definition labels May 5, 2020
@au-z
Copy link
Contributor Author

au-z commented May 5, 2020

Thanks for the pointer! Now I'm wondering, why Vue doesn't make passing by property instead of attribute the default?

Closing since appending .prop solves the problem.

@au-z au-z closed this as completed May 5, 2020
@smalluban
Copy link
Contributor

I'm surprised as well. For example lit-html or angular template framework uses properties over attributes if they exist on the element's prototype 🤷‍♂️

@smalluban smalluban changed the title Vue to Hybrids: Dynamic Props Usage in Vue framework template: passing dynamic data to the property May 5, 2020
@au-z
Copy link
Contributor Author

au-z commented May 5, 2020

You may be interested to know that the . character has been added to Vue as a shorthand for .prop in Vue 2.6

Here's the issue where the proposal was introduced:
vuejs/vue#7582

This makes passing props by property much simpler syntactically:

<my-component .my-prop="myValue" ...></my-component>

Edit: I've tried the shorthand syntax but ran into some trouble. I created a Vue issue to track:
vuejs/vue#11375

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help properties Applies to the property definition
Development

No branches or pull requests

2 participants