Brexit: Wales EU Continuity Bill passed by AMs
A bill to prevent what Welsh ministers call a Whitehall "power-grab" has been passed by AMs.
The Continuity Bill will bring powers over devolved matters currently wielded at EU level to Cardiff Bay.
The draft legislation was fast-tracked through the assembly amid a row between Welsh Labour and UK Conservative ministers over the UK government's Brexit bill.
UK ministers have insisted their proposals will strengthen devolution.
The UK government wants to keep some powers in devolved areas temporarily, saying they are needed to protect the UK internal market.
But the Welsh and Scottish governments say those proposals amount to a "power-grab". Welsh ministers believe the frameworks should be agreed by consensus.
The powers cover areas such as agriculture support and food labelling.
A total of 39 AMs backed the bill, versus 13 against. There was one abstention.
First Minister Carwyn Jones said the law will ensure the system of devolution is preserved.
UK ministers have described the bill as unnecessary, and it was opposed by their Welsh Conservative colleagues in a Senedd debate.
Around an hour after the Welsh Assembly passed its legislation, the Scottish Parliament voted through a similar bill.
Mr Jones told BBC Wales Today that the law protected the Welsh Government's position.
The first minister said the bill "isn't about stopping Brexit".
"What it is about is making sure that the powers the people of Wales voted to give themselves in 2011 are preserved," he said.
"The three governments should agree what powers shouldn't be touched for the next three years, not one government saying to the others what's going to happen."
Mr Jones added: "What we've done today is put legislation in our back-pockets, so we've got that if we need it.
"But I'd rather move forward on the basis of agreement between ourselves, the UK government and the Scottish government."
Analysis by BBC Wales political editor Nick Servini
The UK government will be able to override this legislation if there is no agreement with the devolved administrations, but the fact that it is now in place makes it more difficult for ministers at Westminster to do that.
And the Welsh Government will be acutely aware that trying to overturn the wishes of ministers in Cardiff and Edinburgh is not a good look for a UK government, which has always said it wants to bring people with it on Brexit.
The Welsh Government legislation fills a potential gap after Brexit but it also means Carwyn Jones and Nicola Sturgeon can exert added pressure as all sides try to find agreement.
The first minister insists this is about protecting devolution and not blocking Brexit, but critics insist this was an unnecessary effort that should have been directed towards finding a solution instead.
The Welsh Government said that the UK government's EU Withdrawal Bill as currently drafted would allow Westminster to take control of laws and policy areas that are devolved - a position that was "wholly unacceptable".
The Continuity Bill was backed by Plaid Cymru AMs, as well as most UKIP AMs with the exception of Gareth Bennett.
But Welsh Conservative AM David Melding said: "The Continuity Bill is an unnecessary and unhelpful sideshow which risks undermining the ongoing negotiations with the UK government.
"It was a parody from the start, with members unable to scrutinise its passage through the Assembly in anything like a proper fashion."
"The Welsh Government allowed itself to be carried away by the SNP on a nationalist whim - instead of acting in the national interest," he added.