Javascript通知功能,但没有输出文本



我为Vue应用程序编写了一个通知函数,并将其导入到我的商店(Vuex(中。

export default function getNotificationText(notification) {
// Parent types.
const DRIVE = 'drv'
const DRIVE_GALLERY = 'dgl'
const EXPERIENCE = 'exp'
const EXPERIENCE_GALLERY = 'egl'
const STAY = 'sty'
const STAY_GALLERY = 'sgl'
const LOCATION_PHOTO = 'lpt'
const DRIVE_PHOTO = 'dpt'
const EXPERIENCE_PHOTO = 'ept'
const STAY_PHOTO = 'spt'
const LOCATION_REVIEW = 'lrv'
const DRIVE_REVIEW = 'drv'
const EXPERIENCE_REVIEW = 'erv'
const STAY_REVIEW = 'srv'
const LOCATION_QUESTION = 'lqt'
const DRIVE_QUESTION = 'dqt'
const EXPERIENCE_QUESTION = 'eqt'
const STAY_QUESTION = 'sqt'
const TRIP = 'trp'
const TRIP_PHOTO = 'tpt'
const DRIVE_GALLERY_COMMENT = 'dgc'
const EXPERIENCE_GALLERY_COMMENT = 'egc'
const STAY_GALLERY_COMMENT = 'sgc'
const LOCATION_PHOTO_COMMENT = 'lpc'
const DRIVE_PHOTO_COMMENT = 'dpc'
const EXPERIENCE_PHOTO_COMMENT = 'epc'
const STAY_PHOTO_COMMENT = 'spc'
const LOCATION_REVIEW_COMMENT = 'lrc'
const DRIVE_REVIEW_COMMENT = 'drc'
const EXPERIENCE_REVIEW_COMMENT = 'erc'
const STAY_REVIEW_COMMENT = 'src'
const LOCATION_QUESTION_COMMENT = 'lqc'
const DRIVE_QUESTION_COMMENT = 'dqc'
const EXPERIENCE_QUESTION_COMMENT = 'eqc'
const STAY_QUESTION_COMMENT = 'sqc'
const TRIP_COMMENT = 'trc'
const TRIP_PHOTO_COMMENT = 'tpc'
const USER = 'usr'
const notificationText = ''
// Build user text.
const userText = notification.sender.first_name + ' ' + notification.sender.last_name
// Build action text.
const action = notification.action
const actionText = ''
// actions started
const COMMENT = 'cmt'
const LOVE = 'lov'
const FOLLOW = 'flw'
const ACCEPT_BOOKING = 'apb'
const ADD_PHOTO = 'adp'
const ADD_REVIEW = 'adr'
const ADD_QUESTION = 'adq'
if (action == COMMENT) {
const actionText = 'commented on your'
} else if (action === LOVE) {
const actionText = 'liked your'
} else if (action === FOLLOW) {
const actionText = 'started following you'
} else if (action === ACCEPT_BOOKING) {
const actionText = 'accepted your booking'
} else if (action === ADD_PHOTO) {
const actionText = 'added a new photo on your'
} else if (action === ADD_REVIEW) {
const actionText = 'added a new review on your '
} else if (action === ADD_QUESTION) {
const actionText = 'asked a new question on your'
}
// Build parent text.
const parentType = notification.parent.type
const parentText = ''
if (action === ADD_PHOTO || action === ADD_REVIEW || action === ADD_QUESTION) {
if (parentType === DRIVE_PHOTO || parentType === DRIVE_REVIEW || parentType === DRIVE_QUESTION) {
const parentText = 'drive'
} else if (parentType === EXPERIENCE_PHOTO || parentType === EXPERIENCE_REVIEW || parentType === EXPERIENCE_QUESTION) {
const parentText = 'experience'
} else if (parentType === STAY_PHOTO || parentType === STAY_REVIEW || parentType === STAY_QUESTION) {
const parentText = 'stay'
}
} else if (action === COMMENT || action === LOVE) {
if (parentType === DRIVE_GALLERY || parentType === EXPERIENCE_GALLERY || parentType === STAY_GALLERY || parentType === LOCATION_PHOTO || parentType === DRIVE_PHOTO || parentType === EXPERIENCE_PHOTO || parentType === STAY_PHOTO || parentType === TRIP_PHOTO) {
const parentText = 'photo'
} else if (parentType === LOCATION_REVIEW || parentType === DRIVE_REVIEW || parentType === EXPERIENCE_REVIEW || parentType === STAY_REVIEW) {
const parentText = 'review'
} else if (parentType === LOCATION_QUESTION || parentType === DRIVE_QUESTION || parentType === EXPERIENCE_QUESTION || parentType === STAY_QUESTION) {
const parentText = 'question'
} else if (parentType === TRIP) {
const parentText = 'trip'
} else if (['DRIVE_GALLERY_COMMENT', 'EXPERIENCE_GALLERY_COMMENT', 'STAY_GALLERY_COMMENT', 'LOCATION_PHOTO_COMMENT', 'DRIVE_PHOTO_COMMENT', 'EXPERIENCE_PHOTO_COMMENT', 'STAY_PHOTO_COMMENT', 'LOCATION_REVIEW_COMMENT', 'DRIVE_REVIEW_COMMENT', 'EXPERIENCE_REVIEW_COMMENT', 'STAY_REVIEW_COMMENT', 'LOCATION_QUESTION_COMMENT', 'DRIVE_QUESTION_COMMENT', 'EXPERIENCE_QUESTION_COMMENT', 'STAY_QUESTION_COMMENT', 'TRIP_COMMENT', 'TRIP_PHOTO_COMMENT'].indexOf(parentType) > -1) {
const parentText = 'comment'
console.log(parentText)
}
}
// Combine texts.
const outputText = userText + ' ' + actionText
if (parentText) {
const outputText = parentText
}
// Return output text.
return outputText
}

我想根据if条件显示输出文本userText已经定义为用户firstnamelastname,但我没有得到ActionText或ParentText。if语句内部运行良好,我可以获得console.log,但在if语句外部不起作用。请帮帮我。

以下是代码的粘贴框链接:https://pastebin.com/n0TaXxcJ

// Combine texts.
let outputText = userText + ' ' + actionText
if (parentText) {
outputText = outputText + parentText
}
return outputText

您最终正在覆盖outputText。此外,您将其定义为另一个变量,请改用let。

此外,最好使用config之类的包,而不是到处复制/粘贴const DRIVE = 'drv'和其他包。如果你决定改变一些其他的事情,你必须每年都做出改变。


编辑:

你可以更换

// Combine texts.
const outputText = userText + ' ' + actionText
if (parentText) {
const outputText = parentText
}
// Return output text.
return outputText

用CCD_ 7阻断。如果parentText为空,则不会添加任何内容,对吗?

最新更新