Skip to content

Vue.js 3 #2751

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

Closed
tchukuchuk opened this issue Oct 5, 2020 · 25 comments
Closed

Vue.js 3 #2751

tchukuchuk opened this issue Oct 5, 2020 · 25 comments

Comments

@tchukuchuk
Copy link

Hi,

Do you know where I can find a working example with vuejs 3 and typescript ?

@Vannsl
Copy link
Contributor

Vannsl commented Oct 6, 2020

Hi @tchukuchuk,

just a couple of days ago, I added Vue 3 to a Rails app. I want to create a PR to install vue3 with webpacker and write an article/tutorial about it. Please give me 2-3 days to get that done.

Best
Vannsl

@simonhutchings
Copy link

How did you add it without webpacker? That would work for me for the time being!

@Vannsl
Copy link
Contributor

Vannsl commented Oct 11, 2020

https://dev.to/vannsl/vue3-on-rails-l9d

I just published this, I haven't checked for typos yet ;)
Typescript follows!

@simonhutchings
Copy link

simonhutchings commented Oct 11, 2020 via email

@guillaumebriday
Copy link
Member

v3 installation shouldn't be different from v2

We could close this issue I think 👍

@Vannsl
Copy link
Contributor

Vannsl commented Oct 12, 2020

@guillaumebriday I would suggest to leave it open. I wanted to create a PR to add the installer on webpacker. The installation is different from vue2. The packages are different and the command to create an app.

@Vannsl
Copy link
Contributor

Vannsl commented Oct 12, 2020

(If you just want to close the issues for some reason, it's fine for me. I would still create the PR)

@guillaumebriday
Copy link
Member

I mean the configuration related to webpacker is the same. Only the install has changed.

Since the whole Vue ecosysteme is not ready for 3 yet, I suggest to add a vue3 install option and keep the actual vue option for a bit.

@Vannsl
Copy link
Contributor

Vannsl commented Oct 12, 2020

Hi @guillaumebriday, thanks for the quick reply. I actually propose the same. The vue3 ecosystem is not ready yet, therefore vue3 is behind vue@next - so a vue3-install-option is the way to go

Vannsl added a commit to Vannsl/webpacker that referenced this issue Oct 12, 2020
@guillaumebriday
Copy link
Member

So cool! Thanks 👍

@tchukuchuk
Copy link
Author

Hi @Vannsl ,

Thanks a lot for your article !

Do you know when you'll publish the next one about Typescript support ?

@Vannsl
Copy link
Contributor

Vannsl commented Oct 14, 2020

Hi @tchukuchuk, I plan it to do upcoming weekend.

But as far as I understood it:

  • Add a tsconfig.json in the root directory with settings like:
{
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "strict": true,
    "jsx": "preserve",
    "importHelpers": true,
    "moduleResolution": "node",
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "sourceMap": true,
    "baseUrl": ".",
    "types": [
      "webpack-env"
    ],
    "paths": {
      "@/*": [
        "src/*"
      ]
    },
    "lib": [
      "esnext",
      "dom",
      "dom.iterable",
      "scripthost"
    ]
  },
  "include": [
    "src/**/*.ts",
    "src/**/*.tsx",
    "src/**/*.vue",
    "tests/**/*.ts",
    "tests/**/*.tsx"
  ],
  "exclude": [
    "node_modules"
  ]
}
  • Add lang="ts" to the <script> sections of Vue Single File Components.
  • Add the src/shims-vue.d.ts
declare module '*.vue' {
  import type { DefineComponent } from 'vue'
  const component: DefineComponent
  export default component
}
  • And install typescript via `yarn add -D typescript -D
  • When using ESLint the typescript-eslint packages, too: yarn add -D. @typescript-eslint/eslint-plugin, @typescript-eslint/parser

Since Vue 3 is written in Typescript, there is no need to import other special packages, like the vue-class-component. This enabled the Class Style API. If you're using that in your current codebase, you might have a problem with the migration right now. I Heard that they still want to support the Class Style API but I'm not sure.

As IDE you might need to use VS Code to install the extension "Vetur". It comes with Typescript support for Vue 3.

The rest then should be Vue-TS-syntax realted and is documented on the offical Vue 3 TS Documentation.

@tchukuchuk
Copy link
Author

Thanks ! No need to add and configure ts loader for webpack ?

@Vannsl
Copy link
Contributor

Vannsl commented Oct 14, 2020

That's a good question with Vue 3 already written in TS, I'm not sure.

If it's not working without, the ts-loader config for vue2 was.

{
      test: /\.tsx?$/,
      loader: 'ts-loader',
      exclude: /node_modules/,
      options: {
        appendTsSuffixTo: [/\.vue$/],
      }
},

@pguegan
Copy link

pguegan commented Oct 14, 2020

Hi @Vannsl, and thanks for your great contribution!

While following your instructions (I actually cloned your example repository), I ran rapidly into a major problem. I can't even use a simple @click event / action. For instance, in your HelloWorld.vue component, I just added the following button:

<template>
  <p>
    {{ message }}
  </p>
  <button @click="doStuff">Click me</button>
</template>

<script>
  import { ref } from 'vue'
  export default {
    // ...
    methods: {
      doStuff(event) {
        alert('Hello!')
      }
    }
  }
</script>

All compiles successfully, but then, when clicking the button I get the follow JS error:

HelloWorld.vue:7 Uncaught TypeError: $options.doStuff is not a function
    at Object.onClick._cache.<computed>._cache.<computed> (HelloWorld.vue:7)
    at callWithErrorHandling (runtime-core.esm-bundler.js:225)
    at callWithAsyncErrorHandling (runtime-core.esm-bundler.js:235)
    at HTMLButtonElement.invoker (runtime-dom.esm-bundler.js:352)

Do you think it's related to your setup? Has anyone encountered the same issue?

@Vannsl
Copy link
Contributor

Vannsl commented Oct 15, 2020

Hi @pguegan,

the block methods: {}is part of the "Options API" of Vue 2. It still exists in Vue 3, but I disabled it in the settings. Look for __VUE_OPTIONS_API__ inenvironment.js, set the value to true, restart the webpack-dev-server and try it again. You should see the alert now.

If you want to refactor the code using the setup function of the "Composition API", rewrite the initialisation of the onclick handler to this:

<template>
  <p>
    {{ message }}
  </p>
  <button @click="doStuff">Click me</button>
</template>

<script>
import { ref } from 'vue'

export default {
  name: 'HelloWorld',
  setup() {
      const message = ref('Hello World')

      function doStuff(event) {
        alert('Hello!')
      }

      return {
        message,
        doStuff
      }
  },
}
</script>

@tchukuchuk
Copy link
Author

Hello @Vannsl ,

Have you tried to switch to Typescript ?

@Vannsl
Copy link
Contributor

Vannsl commented Nov 2, 2020

Hi @tchukuchuk,

unfortunately something else came up and I have to work on that first. Did you have the chance to try the setup?

@yanovitchsky
Copy link

Hi @Vannsl, Thanks for the tutorial.
I've successfully installed vue3 with rails but the typescript setup does not compile the shims
TypeScript emitted no output for vue-shims.d.ts
Do you have any idea on why ? Thanks again.

@Vannsl
Copy link
Contributor

Vannsl commented Nov 16, 2020

Hi @yanovitchsky thanks for the reminder, I actually will have time again to look into that. Give me a couple of days and I hope I'll return with a result (also at @tchukuchuk )

t0yohei added a commit to t0yohei/webpacker that referenced this issue Nov 25, 2020
@t0yohei
Copy link

t0yohei commented Nov 25, 2020

I created a pull request (#2792) to use the Vue3 installer like

rails new myapp --webpack=vue3

or

bundle exec rails webpacker:install:vue3

@guillaumebriday
Copy link
Member

As far as I understand from this comment : #2757 (comment)

Installers will be removed, which I think it's a good things because they often do too much or not like in the install guide of every techno we want to install. And it's less thing to maintain for Webpacker contributors

@t0yohei
Copy link

t0yohei commented Nov 26, 2020

Thanks, guillaumebriday. I understand the comment and agree with the way not to use installer but install guide with all installer steps(I guess it could be something like Vannsl wrote).
So I close the pull request.

@Vannsl
Copy link
Contributor

Vannsl commented Mar 17, 2021

Hi everybody (@yanovitchsky, @tchukuchuk), it took a while, but here's how to add TypeScript:

https://dev.to/vannsl/webpacker-vue-3-and-typescript-1i99

@bbugh
Copy link
Contributor

bbugh commented Mar 17, 2021

There is also vite-rails now, which uses vite instead of webpack. It seemed to work very well on the side project I tested it with, and it's wayyyy faster than webpack.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants