2017-05-26 54 views
1

我想設置一個PARAM ID這樣的反應對象:與奧裏利亞路由器真是奇怪的事情PARAMS

reaction: { message_id: ""}; 
    async activate(params) { 
     alert(params.id); 
     this.reaction.message_id = params.id; 
    } 

警報的準確性,以便它提示正確。但後來我收到我的控制檯:

ERROR [app-router] TypeError: Cannot set property 'message_id' of undefined 

編輯:

import { HttpClient, json } from "aurelia-fetch-client" 
import { autoinject } from "aurelia-framework" 
import { Router } from 'aurelia-router' 

@autoinject 
export class message { 

    message: { reactions: Reaction[], id: "" }; 
    editing: boolean; 
    reaction: Reaction; 

    constructor(private http: HttpClient, private router: Router) { 
     this.editing = false; 
    } 

    created() { 
     this.getMessage(); 
    } 

    async activate(params) { 
     alert(params.id); 
     this.reaction.message_id = params.id; 
    } 

    getMessage() { 
     this.http.fetch('message/show', { 
      body: json(this.router.currentInstruction.params.id) 
     }) 
      .then(response => response.json()) 
      .then(data => { 
       this.message = data; 
      }); 
    } 

    update() { 
     this.http.fetch('message/update', { 
      body: json(this.message) 
     }) 
      .then(response => { 
       if (response.status == 200) { 
        swal({ 
         title: "Bericht succesvol geupdatet", 
         type: "success", 
         showConfirmButton: false, 
         timer: 2000 
        }); 
       } 

       this.editing = false; 
      }); 
    } 

    destroy() { 
     swal({ 
      title: 'Weet u het zeker?', 
      type: 'warning', 
      showCancelButton: true, 
      confirmButtonText: 'Ja verwijder dit bericht', 
      cancelButtonText: 'Stop!', 
      confirmButtonColor: '#002e5b', 
     }, (isOk) => { 
      if (isOk) { 
       this.http.fetch('message/destroy', { 
        body: json(this.message) 
       }).then(data => { 
        swal({ 
         title: 'Verwijderd', 
         text: 'Het bericht is succesvol verwijderd', 
         type: 'success', 
         showConfirmButton: false, 
         timer: 3000 
        }); 
       }); 

       this.router.navigate("/dashboard"); 
      } 
     }); 
    } 

    post() { 
     this.message.reactions.push(this.reaction); 

     this.http.fetch('reaction/store', { 
      body: json(this.reaction) 
     }).then(response => { 
      if (response.status == 200) { 
       swal({ 
        title: "Reactie succesvol aangemaakt", 
        type: "success", 
        showConfirmButton: false, 
        timer: 2000 
       }); 

       this.reaction = {}; 
      } 
     }); 
    } 
} 

export class Reaction { 
    message_id: number; 
    account_id: number; 
    reaction: string; 
} 
+0

'reaction'必須是類的一個成員。你的代碼是錯的,所以錯誤是有道理的。嘗試聲明反應作爲類的一個成員,如'反應:{MESSAGE_ID:「」};' –

+0

@FabioLuz它是類的一個成員。只是簡化它,所以它更容易理解。 – Jamie

+0

嗯你可以怎麼整個班? –

回答

1

你只是給人一種類型reaction,而不是一個值。你必須在使用它之前實例化它。試試這個:

reaction: Reaction; //this has a type but it is undefined 
reaction = new Reaction(); //this has a type and it is instantiated! use this line! 

這是一個簡單的Typescript問題,與Aurelia沒有任何關係。