2016-11-17 54 views
0

我正在創建一個將訪問用戶攝像頭的應用程序。如果navigator.getUserMedia失敗,我希望將錯誤變量更改爲錯誤消息。這應該輸出錯誤消息,而是輸出流。我很新的Vue所以原諒我,如果我失去了一些東西很明顯 我的代碼是如果你使用Vue的2.0,ready已被替換mounted如下不改變變量的方法

<template> 
    <div class=""> 
     <h1 v-if="error === null"> 
     Stream 
     </h1> 
     <h1 v-else> 
     {{ error }} 
     </h1> 
    </div> 
</template> 

<script> 
export default { 
    data() { 
    return { 
     error: null 
    } 
    }, 
    methods: { 
    setUnsupported() { 
     this.error = 'Your browser does not support video :(' 
    } 
    }, 
    ready() { 
    this.setUnsupported() 


    if (navigator.getUserMedia) { 

    } else { 
    } 
    } 
} 
</script> 

<!-- Add "scoped" attribute to limit CSS to this component only --> 
<style scoped> 
h1, h2 { 
    font-weight: normal; 
} 

ul { 
    list-style-type: none; 
    padding: 0; 
} 

li { 
    display: inline-block; 
    margin: 0 10px; 
} 

a { 
    color: #42b983; 
} 
</style> 
+0

你可能想檢查這個值,當你設置錯誤,它可能不是你認爲它是。它可能是在不同的範圍上創建一個新的變量錯誤? – Heinrich

+0

我試着給該方法添加一個警告,而且好像根本沒有被調用。我現在很困惑 –

回答

0

。請檢查文檔here

.... 
mounted() { 
    this.setUnsupported() 
} 
....