未捕获的ReferenceError:未定义Firebase Vue JS

时间:2020-01-19 21:43:14

标签: javascript firebase vue.js firebase-cloud-messaging

我有一个订阅按钮,我编写了一个小函数,但遇到上述错误

methods: {
    subscribe(){
        firebase.messaging.requestPermission()
                          .then(() => {
                              console.log('Notification permission granted.');
                              return this.messaging.getToken();
                          })
    }
}

按钮

<button id="subscribe" @click="subscribe">Subscribe</button>

1 个答案:

答案 0 :(得分:1)

1。将firebase安装到您的项目中:

def cross(A,B):
    return [a+b for a in A for b in B]

digits = '123456789'
rows = 'ABCDEFGHI'
cols = digits
squares = cross(rows,cols)
unitlist = ([cross(rows,c)for c in cols]+[cross(r,cols)for r in rows]+
         [cross(rows,cols)for rows in('ABC','DEF','GHI')for cols in('123','456','789')])
units = dict((s,[u for u in unitlist if s in u])for s in squares)
peers = dict((s, set(sum(units[s],[]))-set([s])) for s in squares)
grid = '..3.2.6..9..3.5..1..18.64....81.29..7.......8..67.82....26.95..8..2.3..9..5.1.3..'

def grid_values(grid):
    values =[]
    for c in grid:
        if c == '.':
            values.append(digits)
        elif c in digits:
            values.append(c)
    assert len(grid)==81
    return dict(zip(squares,grid))

def eliminate(values):
    solved_values =[squares for squares in values.keys() if len(values[squares])==1]
    for squares in solved_values:
        digit = values[squares]
    for peer in peers[squares]:
        values[peer]=values[peer].replace(digit,'')
    return values

2。然后在您的项目中添加消息传递模块。例如。 npm install --save firebase 文件。

index.js

3。然后在您的组件中使用消息传递:

import 'firebase/messaging'

请记住,Firebase SDK是模块化的,尽管您引用了import firebase from 'firebase'; ... methods: { subscribe () { const messaging = firebase.messaging(); messaging.requestPermission().then( ... ); } } 包中的所有内容,但是如果您在最终版本中未包含引用模块(在这种情况下为消息传递)捆绑软件,firebase会抱怨它丢失。

在这里查看更多信息: https://firebase.google.com/docs/web/setup