Git Product home page Git Product logo

Comments (12)

andysham avatar andysham commented on May 27, 2024 16

To anyone coming across this thread, a much better solution is:

import { Vue } from 'vue-property-decorator'
import 'vue-router/types/vue'

@Component
export default class MyComp extends Vue {
	click() {console.log(this.$route)}
}

No need to import any runtime classes or declare your own types, you just need to manually import the default vue-router typings.
I had the same issue for the $store property injected by Vuex, and it can be solved in the same way

from typescript-template.

qm3ster avatar qm3ster commented on May 27, 2024 4

You can define a class that includes everything you usually inject.

import {Route} from 'vue-router'

class VueWithRoute extends Vue {
	$route: Route
}

@Component
export default class MyComp extends VueWithRoute {
	click() {console.log(this.$route)}
}

You can define an interface and also reuse it between components.

import {Route} from 'vue-router'

interface WithRoute /* can also extend Vue to make sure nothing is colliding */ {
	$route: Route
}

@Component
export default class MyComp extends Vue implements WithRoute {
	click() {console.log(this.$route)}
}

You can define in the specific component.

import {Route} from 'vue-router'

@Component
export default class MyComp extends Vue {
	$route: Route
	click() {console.log(this.$route)}
}

I basically never end up using the last one because inline definitions are usually less specific than classes and interfaces. eg:

type VueRefs = Vue['$refs']
interface MyRefs extends VueRefs {
	input: HTMLInputElement
}

but inline I could only do

@Component
export default class MyComp extends Vue {
	$refs: Vue['$refs'] & {
		input: HTMLInputElement
	}
}

from typescript-template.

AndrewBogdanovTSS avatar AndrewBogdanovTSS commented on May 27, 2024

@qm3ster thanks for such a broad set of examples, but I have one additional use case I can't figure out. I want to filter values from my store based on id param I'm getting inside $route.params.
So basically it looks like this:
@State(state => state.houses.filter(h => h._id === this.$route.params.id)) house
but this.$route isn't defined although available in mounted hook for example. So my question is how can I get it when I define a property in my class?
Full example

import Vue from "vue"
  import Component from "nuxt-class-component"
  import {Route} from "vue-router"
  import {State, Action, Getter, namespace} from "vuex-class"

  interface WithRoute{
    $route: Route
  }

  @Component
  export default class extends Vue implements WithRoute{
    @State(state => state.houses.filter(h => h._id === this.$route.params.id)) house // Not working here
    mounted(){
      console.log("params:", this.$route.params.id) // Works here
    }
  }

from typescript-template.

hartmut-co-uk avatar hartmut-co-uk commented on May 27, 2024

this in decorator?
Maybe try to do the filtering via a computed property based on the unfiltered @State houses ?

from typescript-template.

AndrewBogdanovTSS avatar AndrewBogdanovTSS commented on May 27, 2024

@hartmut-co-uk you mean smth. like this?

get house(){
      return this.houses.filter(h => h._id === this.$route.params.id)[0];
    }

from typescript-template.

hartmut-co-uk avatar hartmut-co-uk commented on May 27, 2024

yes, looks good to me - is it working?

from typescript-template.

AndrewBogdanovTSS avatar AndrewBogdanovTSS commented on May 27, 2024

yes, it is :) That's the solution I've stopped on after I've logged this issue)

from typescript-template.

kuige avatar kuige commented on May 27, 2024

To anyone coming across this thread, a much better solution is:

import { Vue } from 'vue-property-decorator'
import 'vue-router/types/vue'

@Component
export default class MyComp extends Vue {
	click() {console.log(this.$route)}
}

No need to import any runtime classes or declare your own types, you just need to manually import the default vue-router typings.
I had the same issue for the $store property injected by Vuex, and it can be solved in the same way

@andysham I got the error: Module not found: Error: Can't resolve 'vue-router/types/vue'

from typescript-template.

kuige avatar kuige commented on May 27, 2024

I'm using npm, vue-router is installed, and it can be resolved.

from typescript-template.

ckaminski avatar ckaminski commented on May 27, 2024

Using a default nuxt project I couldn't import vue-router/types/vue at all in my component.
I'll have to try a clean install again - maybe I have something that's out of date...

from typescript-template.

BonBonSlick avatar BonBonSlick commented on May 27, 2024

This does not work
import 'vue-router/types/vue';

all others like


import {Route} from 'vue-router'

interface WithRoute /* can also extend Vue to make sure nothing is colliding */ {
	$route: Route
}

@Component
export default class MyComp extends Vue implements WithRoute {
	click() {console.log(this.$route)}
}

also do not work, $route is undefined everywhere


    import {Component, Vue} from "vue-property-decorator";
    import WithRoute from '@/interface/WithRoute';
    import {Route} from 'vue-router';

    interface WithRoute 
    {
        $route: Route
    }

    @Component({
        components: {
            basic1,
        },
    })
    export default class App extends Vue implements WithRoute {

        constructor() {
            super();
            console.log(this);
            console.log(this.$route);
        }

        /**
         * Choose which HTML DOM layout to use for specific route
         */
        get layout(): string {

            console.log(this);
            console.log(this.$route);
            return 'default_layout';
        }
        mounted() {
            console.log(this);
            console.log(this.$route);
        }

from typescript-template.

BonBonSlick avatar BonBonSlick commented on May 27, 2024

Forgot to add


Vue.use(VueRouter); // <--- this, but what for interface then? 
export default new VueRouter({

but what for interface nad DI then if we tell Vue directly to use it? DI does not work still.

from typescript-template.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.