0

In plain js we could write:

data() {
  return {
    form: {
      name: '',
      password: ''
    }
  }
}

But how to achieve this using typescript (vue-property-decorator)? I've read this should be implemented using interfaces, like that:

interface Form {
  name: string
  password: string
}

@Component
export default class Login extends Vue {
  // how to use interface here?
}

I'm using this as v-model on input:

<input v-model="form.email">

1 Answer 1

1

Your class component can have a property form which is of type Form

interface Form {
  name: string
  password: string
}

@Component
export default class Login extends Vue {
  form:Form = {name: '', password: ''}
}

Didn't test this though!

EDIT

An example of an interface that uses interfaces itself

interface Form {
   users : User[]
   password : string
}

interface User {
   name : string
   id : number
}

let form : Form = {
   users: [
     { name:"Ren", id:3 },
     { name:"Stimpy", id:43 }
   ],
   password: ''
}
Sign up to request clarification or add additional context in comments.

2 Comments

It works, indeed. Could you explain a bit deeper - interfaces are just my like custom types? What if i wanted to nest 1 level deeper?
Yes, interfaces are used as custom types, and you can let Classes subscribe to interfaces too. If your Form contains sub objects you can just make interfaces for those too. I edited the answer with an example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.