isNavigationFailure Function | Vue Router
isNavigationFailure is a utility function that checks if a navigation result represents a navigation failure. This is particularly useful when you need to handle navigation errors programmatically.
📋 Signature
typescript
function isNavigationFailure(
error: any,
type?: NavigationFailureType
): boolean1
2
3
4
2
3
4
🔧 Parameters
error(any) - The navigation result or error to checktype(NavigationFailureType, optional) - Specific failure type to check for
🔄 Return Value
boolean-trueif the error is a navigation failure,falseotherwise
💡 Usage Examples
Basic Usage
javascript
import { isNavigationFailure } from 'vue-router'
router.push('/some-route').catch((error) => {
if (isNavigationFailure(error)) {
console.log('Navigation failed:', error)
} else {
console.log('Other error occurred:', error)
}
})1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
Checking Specific Failure Type
javascript
import { isNavigationFailure, NavigationFailureType } from 'vue-router'
router.push('/some-route').catch((error) => {
if (isNavigationFailure(error, NavigationFailureType.redirected)) {
console.log('Navigation was redirected')
}
})1
2
3
4
5
6
7
2
3
4
5
6
7
In Navigation Guards
javascript
router.beforeEach((to, from, next) => {
// Some guard logic
next()
})
router.afterEach((to, from, failure) => {
if (failure && isNavigationFailure(failure)) {
// Handle navigation failure
console.error('Navigation failed:', failure)
}
})1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
🎯 Common Use Cases
- Error Handling - Distinguish navigation failures from other errors
- Analytics - Track specific types of navigation failures
- User Feedback - Provide appropriate error messages based on failure type
- Recovery Logic - Implement fallback navigation strategies
📝 Notes
- This function works with the
NavigationFailureinterface - When no
typeparameter is provided, it checks for any navigation failure - Navigation failures include aborted, cancelled, and duplicated navigations
- Use with
NavigationFailureTypeenum for precise failure type checking
🔗 Related APIs
NavigationFailure- Interface for navigation failure objectsrouter.afterEach()- Global after hook that receives navigation failures