VueJs获取url查询

时间:2017-02-18 01:14:23

标签: vuejs2 vue-component vue.js vue-router

我正在开发一个带有vuejs的网站,此时我遇到了麻烦,我需要从这样的网址获取url查询(页面),例如这个网站webitename.com/user/?page = 1但是{{ 1}}给我一个错误(Uncaught TypeError:无法读取未定义的属性'page')

有人知道这个问题,可以帮助我吗?

PS:我可以使用

设置查询页面
this.$router.query.page

我可以像

那样得到url normal params
this.$router.push({name: 'userIndex', query: { page: '123' } });

但我无法获得任何查询参数

4 个答案:

答案 0 :(得分:77)

我认为你可以像这样简单地调用,这会给你结果值。

this.$route.query.page

查看图片$ route是Vue Instance中的对象,您可以使用此关键字进行访问,然后您可以选择上面的对象属性:

enter image description here

查看用于选择查询值的Vue-router文档:

Vue Router Object

答案 1 :(得分:5)

@IBOutlet weak var scnView: SCNView! @IBOutlet weak var artInfoView: UIView! @IBOutlet weak var mainTitleLbl: UILabel! @IBOutlet weak var textView: UITextView! @IBOutlet weak var timeLbl: UILabel! @IBOutlet weak var stackView: UIStackView! @IBOutlet weak var artistImg: RoundImage! @IBOutlet weak var artistNameLbl: UILabel! @IBOutlet weak var artistView: UIView! var artRoomScene = ArtRoomScene(create: true) var artImage = UIImage() var artInfo: [Any] = [] var posts = [Art]() var post: Art! var user: Users! var showInfo: Bool = false var showSimilar: Bool = false let alert = Alerts() override func viewDidLoad() { super.viewDidLoad() scnView = self.scnView! let scene = artRoomScene scnView.scene = scene scnView.autoenablesDefaultLighting = true scnView.isJitteringEnabled = true scnView.backgroundColor = UIColor.white if let info = self.artInfo[1] as? Art { let image = self.artInfo[0] as? UIImage let height = (image?.size.height)! / 900 let width = (image?.size.width)! / 900 self.artRoomScene.setup(artInfo: image, height: height, width: width) self.mainTitleLbl.text = info.title let date = info.postDate/1000 let foo: TimeInterval = TimeInterval(date) let theDate = NSDate(timeIntervalSince1970: foo) let time = timeAgoSinceDate(date: theDate as Date, numericDates: true) self.timeLbl.text = "\(time)" self.textView.text = "\(info.artHeight)'H x \(info.artWidth)'W - \(info.price)$ / month - \(info.type) \n \(info.description)." DataService.instance.REF_USERS.child("\(info.userUid)").observe(.value, with: { (snapshot) in if let postDict = snapshot.value as? Dictionary<String, AnyObject> { let key = snapshot.key self.user = Users(key: key, artistData: postDict) if let user = self.user { self.artistNameLbl.text = user.name self.artistImg.sd_setImage(with: URL(string: "\(user.profilePicUrl!)") , placeholderImage: UIImage(named:"Placeholder") , options: .continueInBackground) } } }) } } deinit { print("viewcontroller is being deallocated") } 中存在当前路由属性,this.$route是路由器对象的实例,它提供路由器的配置。您可以使用this.$router

获取当前路线查询

答案 2 :(得分:2)

在我的情况下,我console.log(this.$route)并返回了fullPath:

console.js:
fullPath: "/solicitud/MX/666",
params: {market: "MX", id: "666"},
path: "/solicitud/MX/666"

console.js: /solicitud/MX/666

答案 3 :(得分:0)

您还可以使用纯JavaScript获取它们。

例如:

new URL(location.href).searchParams.get('page')

对于此URL: websitename.com/user/?page=1 ,它将返回值 1