0

Vue2.x vue custom element when i passed param like 111. the dot at the end was removed. Is it well known issue? How to fix it.

Step

  1. npm run build vue file
  2. use file from /dist in php project

Problem in index.php Hello from MyComponent! -> 111

Expected result Hello from MyComponent! -> 111.

Dot at the end was cut off

index.Php file

  <!DOCTYPE html>
  <html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
  </head>
  <body>
    <my-component test-value="111."></my-component>
  <script src="vue-old/dist/main.js"></script>
  </body>
  </html>

MyComponent.vue

<template>
  <div class="my-component">
    <h1>Hello from MyComponent! -> {{ testValue }}</h1>
  </div>
</template>

<script>
export default {
  name: 'MyComponent',
  props: {
    testValue: {
      type: String,
      default: 'default message'
    }
  }
}
</script>

<style scoped>
.my-component {
  color: blue;
}
</style>

main.js to build element

import Vue from 'vue'
import VueCustomElement from 'vue-custom-element'
import MyComponent from './src/MyComponent.vue'
Vue.use(VueCustomElement)
Vue.customElement('my-component', MyComponent)

Vue.component('v-style', {
  render: function (createElement) {
    return createElement('style', this.$slots.default)
  }
})
8
  • Do you have the proper state on Vue side? We first need to see if it's a Vue or PHP issue.
    – kissu
    Commented Jun 20 at 7:08
  • I dont have it. But i try to build in vue3 with cli. param passes correctly. Commented Jun 20 at 7:10
  • Then use Vue3, Vue2 is dead anyway.
    – kissu
    Commented Jun 20 at 7:11
  • Upgrading Vue to a newer version is certainly an option, but due to potential project-wide impacts, it's not the preferred solution at this time. While I haven't personally encountered this exact issue before, it seems to be related to how Vue handles parameters containing a trailing dot. Specifically, the dot is removed when the parameter is interpreted as a number, but remains intact when it's treated as a string. Commented Jun 20 at 7:18
  • "Specifically, the dot is removed when the parameter is interpreted as a number", yes this is how JS behaves. . cannot be a number of any sort, hence keep it as Number. It is feasible if you have for example "111.25" but not without nothing else behind the dot. "Upgrading Vue to a newer version is certainly an option", you are quite late to this one, I recommend migration ASAP to avoid yourself an even harder time later on.
    – kissu
    Commented Jun 20 at 7:25

0