vuejs:有没有办法在组件内部访问router.go()?

时间:2015-10-08 16:35:38

标签: vue.js

我想在组件中调用router.go

目前我设置了window.router = router //所以代码可以正常工作

index.js

const App = Vue.extend(require('./views/app.vue'))
router.start(App, '#app')
window.router = router // I don't want to set this global variable

navbar.vue

methods: {
        search () {
            window.router.go({
                name: 'search',
                query: { q: this.query }
            })
        }
}

我正在寻找的是this.$router.go

navbar.vue

methods: {
        search () {
            this.$router.go({
                name: 'search',
                query: { q: this.query }
            })
        }
    }

谢谢,非常感谢一些帮助

1 个答案:

答案 0 :(得分:39)

#import "NameViewController.h" #import <AVFoundation/AVFoundation.h> @interface NameViewController () @property (strong, nonatomic) AVAudioPlayer *player; @end @implementation NameViewController - (void)viewDidLoad { [super viewDidLoad]; [self createAndStartAudioPlayer]; } - (void) viewWillAppear:(BOOL)animated{ [self.player play]; } - (void) createAndStartAudioPlayer { NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"intro" ofType:@"mp3"]; NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath]; self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL fileTypeHint:AVFileTypeMPEGLayer3 error:nil]; self.player.numberOfLoops = -1; //infinite } 2.x

DEPRECATION WARNING: You didn't set `secret_key_base`. Read the upgrade documentation to learn more about this new config option. (called from service at /Users/Benjamin/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/httpserver.rb:138)

vue-router 1.x

this.$router.push('path')